|
|
| YOUR HIT COUNTER IN PHP |
|
I will in this tutorial show you how to make a simple hit counter, using a
textfile.
|
Making the Hit-counter
|
|
When a user enters your site, you have to update the textfile. I have called
my textfile, mycounter.txt. I first get the current value and then increment this
before I store it. And at the end, I display the viewers that have entered the site.
The code is given below. It is important that you give the textfile write permission.
|
//the filename
$filename = "mycounter.txt";
//open the file
$fileToOpen = fopen($filename,"w");
//set the content of the file into a variable
$content = fread($fileToOpen, filesize($filename));
//check if the textfile is empty
if($content == "")
{
$content = 0;
}
//increment the file
$content++;
//store the file
fputs($content);
//close the file
fclose($fileToOpen);
//display the hits
echo($content);
|
|
Category
|
Programming with PHP
|
|
Author
|
Tommy Johannessen
|
|
|
|