Hit Counter using PHP files

The same file (counter.txt) is used for both reading as well as writing. So each time the file should be closed before opening for the next time as read or write mode.

<?php
$filename=”counter.txt”;
$f=fopen($filename,”r”) or die(”Read Failed”);
$str=fread($f,filesize($filename));
echo “The page has been visited for $str times since 2nd June 2009″;
fclose($f);

$f=fopen($filename, “w”) or die(”Write Failed”);
$str=$str+1;
fwrite($f,$str);
fclose($f);
?>

Comments

Popular Posts