Flat file hit counter with image display
By: Daniel
If you liked the flat file hit counter tutorial, this is a little more advanced. This uses the same basic concept for counting and writing the hits, but is different in showing hits. Use the following code:
$file = "hits.dat"; // Hit count file $fp = @fopen($file, "r"); // Open hits in readonly $count = @fread($fp, filesize($file)); // get hits @fclose($fp); // close file if($count) { $count++; // Add 1 to hits if hits exist } else { $count = 1; // If no hits make hits1 } $fp = fopen($file, "w"); // Open file for writing @fputs($fp, $count); // Write hits @fclose($fp); // close file for($i=0;$i<strlen($count);$i++) { $img = substr($count,$i,1); // Get next image $hits .= "<img src='images/$img.gif'>"; } echo $hits;
If you want to just display the hits without counting them use the following code:
$file = "hits.dat"; // Hit count file $fp = @fopen($file, "r"); // Open hits in readonly $count = @fread($fp, filesize($file)); // get hits @fclose($fp); // close file for($i=0;$i<strlen($count);$i++) { $img = substr($count,$i,1); // Get next image $hits .= "<img src='images/$img.gif'>"; } echo $hits;
Please make sure that your hits.dat file is chmoded to 666, which is all read and write permissions.
You can get as creative as you like with the images used in showing your hits. If you need some basic images you can use the following images:












(5 votes, average: 4.4 out of 5)
August 18th, 2006 at 8:20 am
i use it it woks fine
November 11th, 2006 at 6:01 pm
Nice but how can i set one counter on a profile with [img][/img] it aint working