1

Topic: [Tutorial] Creating a php pageview/visitor counter

In this little tutorial I explain you to create a very simple php pageview counter smile.

|| 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 smile.
<?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 tongue.
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 smile.

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 big_smile.
        $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 smile.

That's all big_smile.

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 smile.
I use this line:
Pageviews: <? include('countscript.php'); ?>

|| Done smile ||
Test it out, and post your reply big_smile.

|| 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 smile.

Last edited by TGYoshi (2010-03-02 13:56:33)

http://tgyoshi.co.cc/signature.png
Am I 'spamming' the forums, am I bored so I go (too) much on the forums or am I just active? O.O

Thumbs up +1 Thumbs down

2

Re: [Tutorial] Creating a php pageview/visitor counter

I am sure it works but I would suggest including a working example. smile

With that said, I am not sure on the security of this since the information is kept in a .txt file chmod'd to 777. There are far better counters in the direct admin, and more others that can be downloaded. This does have further possibilities, if people expand or change things however.

Jeff.

Need support? Feel free to stop by the TeamSpeak 3 server, or contact me on MSN - kimojuno@freebeehosting.com smile.

3

Re: [Tutorial] Creating a php pageview/visitor counter

Of course it is.
This counter isn't "secure" or so, but it works well. If you are running a high-populated website, MySQL and stuff is a lot better, of course.
But fine, this is a simple script to show new php users how it works.

EDIT: Added a sample to see the script in action smile.

Last edited by TGYoshi (2010-03-02 13:56:56)

http://tgyoshi.co.cc/signature.png
Am I 'spamming' the forums, am I bored so I go (too) much on the forums or am I just active? O.O

Thumbs up Thumbs down

4

Re: [Tutorial] Creating a php pageview/visitor counter

As the counter.txt file is never executed, it is in a sense secure. Even if someone managed to modify it they're not going to do any real damage (except ruin your count). The act of adding an integer to the file contents automatically type casts the file contents to an integer anyway (a PHP string casts as 0 in most cases). If someone was worried about loosing the count and want to secure the file they could always just store the counter.txt file outside of the document root. It will be safe and secure there.

Best Regards, George

5

Re: [Tutorial] Creating a php pageview/visitor counter

I'm not that average with Chmod..
Maybe 755 works too? Only admin avaible , the script is runned on the same server or something like the .txt.

Maybe.. tongue

http://tgyoshi.co.cc/signature.png
Am I 'spamming' the forums, am I bored so I go (too) much on the forums or am I just active? O.O

Thumbs up Thumbs down