This tutorial will show you how to do a simple PHP based hit counter using a text file to store the number of hits. This will add +1 whenever someone opens the page.
I explain in the code how it works.
Code:
<?php
// The location of the textfile
$filename = "hits.txt";
// Opens the file and makes it into an array
$arr = file($filename);
// $arr[0] is the first row of the textfile (the number
// of hits), this adds +1 to the hits
$arr[0]++;
// Now open the textfile for writing
$fh = fopen($filename, "w");
// Writes the number of hits to the textfile
fwrite($fh, "$arr[0]");
// Closes the textfile
fclose($fh);
// Shows number of hits
echo $arr[0];
?>
All you do is copy this piece of code, save it to hitcounter.php (or anything).
Then make a textfile called hits.txt.
Upload the files to your ftp and chmod hits.txt to 777.
To add the counter to your page, add this code:
<?php
include("hitcounter.php");
?>