Flat file hit counter
By: Daniel
So you need to track hits to your website but don’t want to use a database or don’t have access to a database server. You can still do this by using a flat file on your server. Copy the following code to your website where you want to show/count the hits.
$file = 'hits.dat'; // File where data is stored $fp = fopen($file, "r"); // Open file as read only $count = fread ($fp, filesize($file)); // Read hits fclose($fp); // Close file $count++; // Add 1 to the count $fp = fopen($file, "w"); // Open file for writing fwrite($fp, $count); // Write the count fclose($fp); // Close file echo "Load $count times";
If you wanted to only show the code without updating the hits use the following code:
$file = 'hits.dat'; // File where data is stored $fp = fopen($file, "r"); // Open file as read only $count = fread ($fp, filesize($file)); // Read hits fclose($fp); // Close file echo "Load $count times";
Please note that the hits.dat or the file in the $file variable must be chmoded to atleast 666 or be read and writable.



(5 votes, average: 4 out of 5)
May 3rd, 2008 at 5:36 pm
Many Thanks. Great site, keep up the good work.