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.

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

<?php
$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 55 Votes | Average: 4 out of 55 Votes | Average: 4 out of 55 Votes | Average: 4 out of 55 Votes | Average: 4 out of 5 (5 votes, average: 4 out of 5)
Loading ... Loading ...
del.icio.us:Flat file hit counter digg:Flat file hit counter spurl:Flat file hit counter wists:Flat file hit counter simpy:Flat file hit counter newsvine:Flat file hit counter blinklist:Flat file hit counter furl:Flat file hit counter reddit:Flat file hit counter fark:Flat file hit counter blogmarks:Flat file hit counter Y!:Flat file hit counter smarking:Flat file hit counter magnolia:Flat file hit counter segnalo:Flat file hit counter

One Response to “Flat file hit counter”

  1. codedone Says:

    Many Thanks. Great site, keep up the good work.

Leave a Reply

eXTReMe Tracker
geovisitors