Quando clicco sul pulsante "Visualizza file" incremento la visita, cosa che non vorrei che accadesse. Vorrei che il numero dentro il file, ovvero quello stampato sulla pagina per mezzo della variabile $VisiteNonUniche, si incrementasse solo quando si accede direttamente alla pagina o tutt'al più quando l'utente esegue un reflesh della pagina stessa e non anche quando ci si ritorna cliccando sul pulsante "indietro" del browser. Se imposto un header che esegue il download del file il problema non svaniche.
Codice PHP:
<?php
class Problema {
public function Leggi($file) {
if (file_exists ( $file )) {
header ( 'Content-Description: File Transfer' );
header ( 'Content-Type: text/plain; charset=UTF-8' );
header ( "Expires: on, 01 Jan 1970 00:00:00 GMT" );
header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
header ( "Cache-Control: no-store, no-cache, must-revalidate" );
header ( "Cache-Control: post-check=0, pre-check=0", false );
header ( "Pragma: no-cache" );
header ( 'Content-Length: ' . filesize ( $file ) );
readfile ( $file );
exit ();
}
}
public function Download($file) {
$this->Leggi ( $file );
}
public function PreparaDownload() {
$variabilePOST = "x";
if (isset ( $_POST [$variabilePOST] )) {
$file = "file.txt";
$this->Download ( $file );
}
}
public function VisiteNonUniche() {
$file = "file.txt";
$v = 1;
if (file_exists ( $file )) {
$v = file_get_contents ( $file ) + 1;
}
file_put_contents ( $file, $v, LOCK_EX );
return $v;
}
}
$Prova = new Problema ();
$VisiteNonUniche = $Prova->VisiteNonUniche ();
$Prova->PreparaDownload ();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Problema</title>
</head>
<body>
<p>Visite: <?php print_r($VisiteNonUniche)?></p>
<form action="" method="post">
<input type="submit" value="Visualizza file" name="x">
</form>
</body>
</html>
Grazie