Wanted to make your own hit counter and have it displayed to your visitors? This copy and paste tutorial tells you exactly how to create such a file and output a user friendly generated number.
<?php
// Rename HitCounter.txt to the hit counter file
$HitCounterName = "HitCounter.txt";
// Attempt to openthe file + error control
if($hitCount = @file_get_contents($HitCounterName))
{
$readHitFile = @fopen($HitCounterName, "w+")
if($hitCount === NULL)
$hitCount = 0;
// Check the file for writing + error control
if(is_writable($HitCounterName))
{
// Attempt to write to file + error control
if(@fwrite($readHitFile, $hitCount+1) === FALSE)
echo "Cannot write to hit counter file.";
}
else
{
echo "Cannot write to hit counter file.";
}
// Output total hits in a nice number format
echo number_format($hitCount);
@fclose($HitCounterName);
}
else
echo "Cannot read hit counter file.";
?>
You might like to change the variable
$HitCounterName. This variable contains the location to your hit counter file. For the time being, I'll call it
HitCounter.txt. You should create a file called
HitCounter.txt and upload it to your web server then chmod it
666 (read write, read write, read write).
Once you execute it once the counter file will be updated and numbers will gradually increase.
If you have multiple pages in different folders and wish to count how many hits those pages also receive, please change the file variable
$HitCounterName to your document root + the hit counter name. Such as:
/home/me/public_html/HitCounter.txt
If you think that code is really too much or just prefer slicker code, put the above code in to a separate PHP file
(like myCounter.php) and then include it in to your pages like so:
<?php
include("/path/to/myCounter.php");
?>
Posted by Steven Sullivan on 9th December, 2006 - 16:10:18 GMT
There are no comments. Why not be the first to comment?