page view counter in php

Creating a visible page counter is not as hard as it may sound. Like most sites, I have a database to monitor traffic, however it seems like major overkill to make DB requests and updates just to display a visitor count on a page. It is much simpler to have a hard file which maintains this count for you.

I am using a count.dat file, but if you want to track this for multiple pages, it is best to name multiple files such as hp_count.dat, contact_count.dat etc (bear in mind that if you have too many pages all doing this, you will end up with a plethora of dat files to be referenced, so tidy them into their own folder). The code below inserted into a page counts the visitors and display the results dynamically.

<?php
if(file_exists("count.dat")) {
    $exist_file = fopen("count.dat", "r");
    $new_count = fgets($exist_file, 255);
    $new_count++;
    fclose($exist_file);
    // to be invisible counter comment out next line;
    print("$new_count people have visited this page");
    $exist_count = fopen("count.dat", "w");
    fputs($exist_count, $new_count);
    fclose($exist_count);
}
else {
    $new_file = fopen("dat_folder/count.dat", "w");
    fputs($new_file, "1");
    fclose($new_file);print("you are visitor number 1"); 
}
?> 

To insert the code to a page where you want the counter displayed you would use the following code:

<?php require("count.dat"); ?> 

This is a very basic script which just counts the hits to a single page but, as I said above, you can reuse it easily to track multiple pages.

Counting only bookmark and type-in traffic

Using the $HTTP_REFERER variable we can determine in most cases where a visitor has come from. The reason we can’t guarantee this all the time is because of anonomizer programs which spoof or block this environment variable from recording accurate information. For this example I am being simplistic, so I have ignored the potential problems associated with this. When a surfer bookmarks your page or types your page URL into the browser window the $HTTP_REFERER variable will usually be blank.

So if I only wanted to count bookmarkers to a page I would add the following lines of code before the counting routine to the code above:

if($HTTP_REFERER != "") {
    //add one to the count and update the count.dat file
} 

If you add the code above you’ll see it will only increment the counter if you type the URL into your browser or come from a bookmark. You can also hit refresh after you have typed in or come from a bookmark to increment the counter.

However, if you come from any hyperlink your visitors page count will not be incremented. Remember that the != means not equal to; and when you say != "" it means that as long as there is something there, they must have come from other means than bookmark or type-in. I sometimes notice the words "bookmark" you could also add the regular expression to ignore case and look for the work bookmark by adding a second loop inside the above one:

if(eregi("bookmark", $HTTP_REFERER) {
    // I found the word "bookmark" so don't count this visitor
} 

So there you have it, a quick and simple visitor counter in PHP, along with some directions for adding a lot more functionality to it.

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: