Topic: [Tutorial] Creating a php pageview/visitor counter
In this little tutorial I explain you to create a very simple php pageview counter
.
|| Setting up ideas ||
So, how are we going to do it?
To keep it simple, just a plain .txt file with a number in it. One number get added as the page gets loaded, and the current number will be shown (echo'ed).
|| Creating files ||
For this we need two files. A .txt file with holds the number and a .php file with holds the script.
I call the .txt file 'counter.txt' and the .php file 'countscript.php'. (No qoutes, of course)
Don't forget to give the .txt file 777 permissions, otherwise we can't edit it!
|| counter.txt ||
Pretty easy with comes in here. Just the number '0'.
|| countscript.php ||
Now the hard part begins O_O.
Every php script must be between <?php (or <? ) and ?> tags. This one is no exception.
To be sure that the counter.txt file exist, we create an if-condition to check it.
if (file_exists('counter.txt'))
The function file_exists(string file) is very useful for this job. It will return false or true. But what if the file does not exist? We also need an else. I'll write the whole script until now below
.
<?php
if (file_exists('counter.txt'))
{
}
else
{
}
?>
Ah, great. What if the file does not exist? We can put 'echo('It seems that the file does not exist');' in there, but that's too simple
.
We create it there.
$fil = fopen('counter.txt', w);
fwrite($fil, 1);
echo '1';
fclose($fil);
Using that the file will be created. The variable $fil will open the file counter.txt with writing rights (need 777!), the 2nd line will write in the file that the number is 1, the third one will echo the 'value'. It's just 1, so why read it if we already know it? - and finally we close the file and end the script
.
If the file DOES exist, we'll use this. I'll explain everything in the comment lines.
$fil = fopen('counter.txt', r); // Open the file, just using reading rights
$dat = fread($fil, filesize('counter.txt')); // Read the value of the file (see php documentation for explanations about the function)
echo $dat+1; // We will echo the number, and add one by the number because it's a new visitor O_O.
fclose($fil); // Close the file, we echo'ed it
.
$fil = fopen('counter.txt', w); // Open the file again with writing rights
fwrite($fil, $dat+1); // Update the file, by adding one to the value we got earlier.
fclose($fil); // We wrote it, now close it
.
That's all
.
Full contain of the countscript.php file: (no comments)
<?php
if (file_exists('counter.txt'))
{
$fil = fopen('counter.txt', r);
$dat = fread($fil, filesize('counter.txt'));
echo $dat+1;
fclose($fil);
$fil = fopen('counter.txt', w);
fwrite($fil, $dat+1);
fclose($fil);
}
else
{
$fil = fopen('counter.txt', w);
fwrite($fil, 1);
echo '1';
fclose($fil);
}
?>
|| Now let it workkkk! ||
The only thing we have to do is include the script on your pages
.
I use this line:
Pageviews: <? include('countscript.php'); ?>
|| Done
||
Test it out, and post your reply
.
|| Sample ||
If you want to see the script in action you can visit my website (Click), in the footer you can see a pageview information
.
Last edited by TGYoshi (2010-03-02 13:56:33)






