Counters are required to count the number of visitors coming to the page or the browsers open the number of time the page. The PHP script we will be discussing here is one simple page counter and it will not take in to account if same visitors in same session opening the page again and again. This will also not check the IP and increase the count with unique IP only. These are part of advanced counter script we will discuss separately. We will discuss here one simple page counter or hit counter.
We will use flat text file to store the data ( count of the page ) and will not use any database here for simplicity.
For a better understand on how to read from a file and on how to write to a file, please read the articles here before working on the counter script.
We will use the page counter.php to store our script and we will keep our data ( counter value ) in the text file counter.txt. Both the files will be in same directory.
Create one text file in the same directory of the page where the counter to be displayed and give the name counter.txt. If you are using this script in Linux system then give write permission to this file. If you are testing this script in your local window machine then this may not be required as by default write permission will be there. If you are running this script in a windows hosting account then ask the administrator to give write permission to this file. This is important as we will be writing this file with new data on every page request of the main file.
Here is the code to open the counter.txt file and then displaying the data by incrementing it by one. We will use file read function fread() here to read the data from the text file. Before that we will use fopen to open the file in reading mode.
$filename = "counter.txt"; // This is at root of the file using this script.
$fd = fopen ($filename, "r"); // opening the file counter.txt in read mode
$contents = fread ($fd, filesize($filename)); // reading the content of the file
fclose ($fd); // Closing the file pointer $contents=$contents+1;
// incrementing the counter value by one echo $contents;
// printing the incremented counter value
/*
The above code will do the reading and displaying the counter to the screen,
now we have to store the above value in the same counter.txt file by overwriting
the old data with the new counter data. We will open the counter.txt file in
write mode and then write to it.
*/
$fp = fopen ($filename, "w"); // Open the file in write mode fwrite ($fp,$contents); // Write the new data to the file fclose ($fp); // Closing the file pointer
That's all, very simple. You can create counter for any page like this. But we will move to next step. We will try to create attractive graphical counter or call it digital counter using the same script but adding little more to display images in place of numbers digits. Like this...
Page hit counter by using digit images for graphical display
We will not discuss how the counter should work, how the data is to be read and written to the text file. We will start from the incremented data or the data, which we are displaying as, counter value and work on it to display image counter.
In that script the variable $contents store the value of the present value of the counter. We will start from there.
Here to display a graphical counter we have to call the digits. We will have graphical digits from 0 to 9 in different styles stored in different directories. To display in particular style we will call the digits of the directory.
To display the number 502 we will call or display the digit 5.gif then 0.gif and then 2.gif. This will make the graphical counter. Same way to display 758 counter value we will call first 7.gif then we will call 5.gif and then we will display 8.gif. We have kept different style of digits in different directories so by changing the directory name we can change the style of the digits.
Now let us find out how to know what digit to display by reading the number. There can be many ways here. One is to use string functions to identify the digits in the number. We will use mathematical functions to do that here. You can develop your own way to do this also. As our number system is of base 10 so dividing a number by 10 and collecting the reminder will give us the right most digit. So if we divide 502 by 10 then we will get 2 as reminder and we can store it in an array. Then the quotient can again be divided by 10 to get the next number( 50/ 10 will give reminder 0 ) and we will do this till we get all the digits. Here is the code to do this.
$cdisp=$contents; // Storing the counter value in another variable $divisor=10; // setting the divisor value to 10 $digitarray=array(); // creating an array
do {$digit=($cdisp % $divisor); // looping through the till all digits are
taken $cdisp=($cdisp/$divisor); // getting the digits from right side array_push($digitarray,$digit); // storing them in the array } while($cdisp >=1); // condition of do loop
// array is to be reversed as digits are in reverse order $digitarray=array_reverse($digitarray);
$dir="d5"; // setting the direcotry value. for different styles
while (list ($key, $val) = each ($digitarray)) { // looping through the array echo "<img src='digits/$dir/$val.gif'>"; // calling one by
one digits based on the value of the array } // end of the loop