A Simple Text-File Hit Counter
<?php
function retrieveCount($hitfile) {
$fr = @fopen($hitfile, 'r');
if(!$fr) {
$count = 0;
} else {
$count = fgets($fr, 4096);
fclose($fr);
}
$fr = @fopen($hitfile, 'w');
if(!$fr) return false;
$count++;
if(@fputs($fr, $count) == -1) return false;
fclose($fr);
return $count;
}
$count = retrieveCount('hitcount.dat');
if($count !== false) {
echo "This page has been visited $count times<BR>";
} else {
echo "An Error has occurred.<BR>";
}
?>
Related examples in the same category