The following PHP function will store page hits on a file using the fopen, fwrite functions. It will return the value in a variable which can be used to output image numbers.
For example <img src="<?=$value;?>.gif" title="<?=$value;?> hits" /> which can be 0.gif.
It's not a complex code, don't rely on it for exact data or for unique visits and stuff like that. It's just a simple hit counter ready to do it's simple task.
<?php
function hitcount()
{
$file = "./include/counter.txt";
if ( !file_exists($file)){
touch ($file);
$handle = fopen ($file, 'r+'); // Let's open for read and write
$count = 0;
}
else{
$handle = fopen ($file, 'r+'); // Let's open for read and write
$count = fread ($handle, filesize ($file));
settype ($count,"integer");
}
rewind ($handle); // Go back to the beginning
/*
* Note that we don't have problems with 9 being fewer characters than
* 10 because we are always incrementing, so we will always write at
* least as many characters as we read
**/
fwrite ($handle, ++$count); // Don't forget to increment the counter
fclose ($handle); // Done
return $count;
}
?>
Usage:
<?php
if(!isset($_GET[page]))$number=hitcount();
else
{
$file="./include/counter.txt";
$handle = fopen ($file, 'r+'); // Let's open for read and write
$number = fread ($handle, filesize ($file));
settype ($number,"integer");
}
//echo "<h2>Visitor nr:</h2>";
for($i=0; $i<=7-(strlen($number)); $i++) echo "<img border=\"0\" src=\"counter/style1/0.gif\">";
for($i=0; $i<(strlen($number)); $i++)
{
$numb=substr($number, -((strlen($number))-$i), 1);
echo "<img border=\"0\" src=\"counter/style1/".$numb.".gif\">";
}
?>