<?php
$myfile = "totalhits.txt";
//Assigns file name to the variable we'll use to handle it
if(file_exists($myfile))//if the file exists
{ // runs counter script
$var = fopen( $myfile,'r+');
//opens in read and write mode for file
$visits = fread($var,filesize($myfile));
//puts the content of the file for its whole length
rewind( $var );
//resets the file position indicator to the beginning
$visits++; //increments the actual number of vists by 1
fwrite($var, $visits);
//writes on the variable the actual (incremented) value
fclose($var);//closes our file reference
}
else
{
print "File $myfile doesn't exist...";
Die();
//if the file doesn't exist prompts a warning and kills the script
}
$message = sprintf("Total Hits: %s",$visits);
//saves our visits message in a variable ($message) that will be used as output
print $message;
?>