PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Guts or Glory Poker PHP - A casino-style card game written entirely in PHP


[back]
WB01624_.gif (281 bytes) 01/09/00 "Tracking Site Visitors" WB01626_.gif (272 bytes)[next]

Page counters

Creating a visible page counter is something I'd like to explore next. I have a stats program which tells me how many visitors I have already for each page that is essentially invisible, but I have received a number of emails expressing interest in how to make a counter using PHP that is visible. The code below inserted into a page counts the visitors and display the results dynamically.

<?
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("count.dat", "w");
fputs($new_file, "1");
fclose($new_file);
}
?>

Example #24: 4502 people have visited this page

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

<? require("/path/to/count.dat"); ?>

The first time you load your page you may think it isn't working, but if you look at the code above you'll see that it checks for the existence of count.dat and if it isn't there it creates the file for you, using the file_exists function. If count.dat doesn't exist then it will create it with the counter set as one. I could easily add a line to it that prints "you are visitor number 1" or something perhaps more dramatic, but I chose to leave it be. Admittedly this is a very basic script for counting the hits to a single page. Now what if we only wanted to count say, bookmarkers or type-ins?

Counting only bookmark and type-in traffic

Using the $HTTP_REFERER variable we can determine in most cases where a page visitor has come from. The reason I don't say all the time is because there are anonomizer type programs which will spoof or block this environment variable from recording accurate information as a means of providing the surfer some better security. So don't make it a habit to put all your programming eggs in the $HTTP_REFERER variable. However, for my example here, I am going to do 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 != "")
{
// go ahead and add one to the count and update the count.dat file
}

Try adding the code above and 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
}

Please vote on what you think of this diary entry :)

How useful was this diary entry? Avg Surfer Rating: 4.18 (566)

[back]WB01624_.gif (281 bytes) 01/09/00 "Tracking Site Visitors" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 12/11/01 07:45