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:

<?php
$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:

<?php
$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:
0.gif1.gif2.gif3.gif4.gif5.gif6.gif7.gif8.gif9.gif

1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 4.29 out of 5)
Loading ... Loading ...

2 Responses to “Flat file hit counter with image display”

  1. I-RoBoT@Corp. Says:

    i use it it woks fine

  2. Xfuryion Says:

    Nice but how can i set one counter on a profile with [img][/img] it aint working

Leave a Reply

geovisitors