This simple hit counter is a really simple to setup and no MySQL database is needed.
To begin, paste the following in a new document:
<?php
// This is the file where it tracks the hits
$file = "counter.txt";
// You shouldn't need to update anything else below.
// Check $file is writable
if(is_writable($file)){
// Get contents of $file
$fcontent = file_get_contents($file);
// Check file content -> register new hit value
if(!empty($fcontent)){
$ncontent = $fcontent+1;
}else{
$ncontent = 1;
}
// Open file and write in new amount
$fhandle = fopen($file, "w+");
$write = fwrite($fhandle, $ncontent);
fclose($fhandle);
// Close file stream
// Check new info writen correctly and then echo the new value
if(!$write){
echo("error updating counter");
}else
}
}else{
echo("- Check ".$file." exists.<br />");
echo("- Check the file is set to 777 permissions.");
}
?>
Save it as
counter.php.
Next, make a blank document and save it as
counter.txt.
Now upload both
counter.php and
counter.txt onto your server.
You now need to change the permission of
counter.txt.
Chmod
counter.txt to 777.
In order to display the hits use this code:
<? include("counter.php"); ?>
And.. you're done! It's that simple.