PHP Counter Tutorial
Blake McDonald on 2001 April 14
    

This tutorial will show you how to make a very simple PHP counter. Every time the page loads with the code on it, it will count up one hit

.

Ok now on to the counter. This is what we will be writing today.


<?php

// Full sys path to count.txt
$counter_file "/full/sys/path/counter/count.txt";

// Puts the file lines into $counter_file_line array
$counter_file_line file($counter_file);

// adds one to the number in the [0] array or the very first line
$counter_file_line[0]++;

// opends the file and then writes the new number and then closes it
$cf fopen($counter_file"w");
fputs($cf"$counter_file_line[0]");  
fclose($cf);
 
// prints out the full count
echo "$counter_file_line[0]";


?>


Okay...so it is somewhat confusing right now, but don't worry..we'll go over it step by step.

First things first. The counters total number has to be stored somewhere right? Other wise how will the computer know how many people have been to the site? For this counter we will use the text file for the storage. You have to tell the server which file to use. So...simply include the path to your text file. This may look something like this....



/home/sites/site123/web/counter/count.txt


or


/usr/home/username/public_html/counter/count.txt


or even


/public_html/counter/count.txt 



Note..you would be creating the counter directory..and uploading a blank text file called count (count.txt) to that direcory. So..that file path goes in here:


<?php
$counter_file 
"/full/sys/path/counter/count.txt";

?>


That will be the first line of the program (aside from the < ?...which you did remember to start your program with..right?). What this does is makes the '$counter_file' variable equal to the system path to the log file that the count.txt file is stored in.



The next line of the program is:


<?php
$counter_file_line 
file($counter_file);

?>


Okay..so what does this do? Lets break it down. The first thing we notice is the file() function. This is a small script that is part of PHP. file() takes a file on the server, and reads this file. In reading this file...it breaks the file down into a series of variables called arrays. Essentially, each array is the same name, except that they are differentiated by a number.

What this file() does in this program is open up the $counter_file and then puts each line of the file into an array. For example:


<?php
$counter_file_line
[0]
// that would be the very first line in the text file
$counter_file_line[1]
// that would be the second line in the text file
$counter_file_line[2]
// The third and it goes on like that.

?>


The count.txt file will only have one line to it...thereby making the array variable $counter_file_line[0] . Why $counter_file_line as the name? Because, if you look at the code...file($counter_file) is equal to $counter_file_line. Since the file() function returns an array, or multimple values, it knows that the base name will be $counter_file_line with [array.

The next line is this


<?php
$counter_file_line
[0]++;

?>


What this does is get what's on the very first line of the text file and adds 1 to it. The ++ at the end means to add one to the number that's on the first line of the file. This would do the exact same thing as this code does...


<?php
$counter_file_line
[0] = $counter_file_line[0] + 1;

?>


...which is basically just adding 1 to whatever $counter_file_line[0] is equal to at the time..and making it equal to that sum. We use just the ++ at the end instead because it's shorter.

The next lines are this


<?php
$cf 
fopen($counter_file"w");
fputs($cf"$counter_file_line[0]");
fclose($cf);

?>


Looks confusing right? Well I will go through it step by step.



The first line is


<?php
$cf 
fopen($counter_file"w");

?>


What this does is opens the $counter_file, at the path we put above. The "w" means that it will open it and erase it.

The next line is this


<?php
fputs
($cf"$counter_file_line[0]");

?>


The fputs() function writes to the file. The $cf is what file to write to, and the $counter_file_line[0] is what to write. Remember when we opened this file and added one to the number in it? Well "$counter_file_line[0] was the number we added to and now we just wrote that new number to the file.

The last line is


<?php
fclose
($cf);

?>


All this does is closes the file.

And last we print out the counters total


<?php
echo $counter_file_line[0];

?>


What this does is simply prints out the first line that we took out of the file above and added one to. It's also the same thing we just wrote to the file. The "echo" tells it to print that for people to see in the browser. You can also use "print" in its place and it does the same thing, it's all a matter of what you like best.



In the end your counter should look like this, I have added comments to this one so you can see what everything does step by step.



<?php

// Full sys path to count.txt
$counter_file "/full/sys/path/counter/count.txt";

// Puts the file lines into $counter_file_line array
$counter_file_line file($counter_file);

// adds one to the number in the [0] array or the very first line
$counter_file_line[0]++;

// opends the file and then writes the new number and then closes it
$cf fopen($counter_file"w");
fputs($cf"$counter_file_line[0]"); 
fclose($cf);

// prints out the full count
echo $counter_file_line[0];

?>


So, how do you use this counter. Real easily...simply insert this code:


<?php
include("/path/to/counter.php");  
?>


into the page where you want the Counter to show up. Its that easy.
Tentatively planning to Open Soon! (no dates ...) // Doing heavy development now...