14 December 2009

Simple Text File Hit Counter

A simple hit counter using a text file. The script records every page hit into the text file called log.txt. The total number of hits is then displayed when the counter script is called.

You must create a blank file called log.txt with an initial value of 0.

<?php

$fp = fopen("log.txt", "r");
$count = fread($fp, 1024);
fclose($fp);

$count = $count++;
echo "Total Hits: $count";

$fp = fopen("log.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>


Assuming you call the script counter.php, then you can call the script by using:
<?php include_once('counter.php'); ?>

0 comments:

Post a Comment