Site Navigation:

Home // Tutorials // PHP // The Counting Tutorial

The Counting Tutorial

1

The Text Counter

Want to keep track of how many hits are on each page? This tutorial shows just how to do that. The script is easy, and you can use it as long as you have write permissions. Heres the full code:

<?php
$filename= "hits.txt";
$fd = fopen ($filename , "r") or die ("Uhoh! The PHP script cant open $filename");
$fstring = fread ($fd , filesize ($filename));
echo "$fstring";
fclose($fd);
$fd = fopen ($filename , "w") or die ("Uhoh! The PHP script cant open $filename");
$fcounted = $fstring + 1;
$fout= fwrite ($fd , $fcounted );
fclose($fd);
?>


The first thing we do is start our block of PHP code using the <?php tag. Then we set a variable named $filename to store the location of where our textfile is. You'll need to open up a file, and type in 0. Then save it as something like 'hits.txt' or 'counter.txt'.

The second line opens up the file that was specified in $filename. We put "r" for the second argument so we can read the file first. After the function we put or die ("message here") so if there is an error, PHP will just say the message and not an ugly error/warning message.

Now we read the file with the arguments $fd and filesize($filename). $fd is the variable for the filestream we are using. We use filesize to get the size of the file in bytes to read. Then we use echo to print out the number of hits we have. Since we are neat we close it using fclose($fd).

Now we have to add this hit to the text file. We open the file again, except the only difference is that we used "w" instead of "r", the "w" means to write. Now we take the number of hits in the file, and add 1 to it inside a variable named $fcounted. Then we write to the file using fwrite. $fd is the filestream we're using, and we put $fcounted on the second argument to specify what to write. Then we finish it up by closing the stream.
2

Unique IP Counting

Sure, that counter works great, but what if you don't want duplicate IP's counted? This time the code looks a little different.

<?php
$filename = "hits.txt";
$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;
$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);
?>


There are a few familiar and a couple new functions in this script. This time, we dont actually use a number, but log IP's. This is so we can keep track of what ip's have been to the page and which have not. First off, we specify the file name. Then this new function named file() appears. This functions loads any ASCII file into an array by each line. Then, we reset the variable and use array_unique() to remove all the duplicate functions from the array.

So, now we have an array with all the IP's in the site w/o the duplicate ones. What is the purpose of this? Now we can count how many items are in the array and output how many IP's have been on the site, in other words, the number of hits on the site. On the next few lines, everything is the same as the regular one, except, the echo is removed, and $fcounted is modified a little. Instead of $fstring + 1 in $fcounted, we use $fstring . "\n" . getenv("REMOTE_ADDR").

Lets piece it together. The $fstring part is the current value of the text file. Then we put . "\n" to add a new line. The period means to glue a string together with a function or variable. See http://www.php.net/manual/en/language.operators.string.php. The \n means to print a new line. Then we use another period to concatenate it with getenv("REMOTE_ADDR"). getenv("REMOTE_ADDR") returns the IP of the current user. Another way to return the IP if register globals is on is by $REMOTE_ADDR.


This tutorial was written by 02
Visit Codexx (my site) at codexx.tk.
Codexx link
spacer

This Tutorial was by Codexx, please post any questions in the forum. Thank you.
BACK TO TUTORIALS



Home Tutorials Free Stuff About Gallery Forum Links Contact

Copyright © Robouk 2001.

Dreamweaver®, Flash® and Photoshop® are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries.
Hosted by Pixelcode - web design tutorials.