Feb 24, 2014

Easy PHP Hit Counter Tutorial

A hit counter is simply a script which counts the number of times web pages have been accessed. Generally, this is essential since it allows both the visitors and the owner of the web page to be aware of how the page is popular. A simple hit counter can hassle free be created with PHP in just 9 lines of code. 
Note that this counter will count only the number of times the web page has been accessed, although, it will fail to count the number of the unique visitors. 
The Basics
A simple PHP HIT counter is meant to easily update the text file, that often keep the track of the number of times the page has been accessed. Typically, the PHP script is supposed to read the current number present in the file. It then adds digit 1 to the number then writes to the file the number and finally closes the file. 
The script then is embedded into the html on the page in order to be easily executed each time the web page is accessed. 
The Script
A simple hit counter script basically looks like this: 
>?php$ filename= “hits.txt” GO count =file ($ filename) Go$ count {0} = = GO file= fopen ($ filename, “w”) or die (“Cannot find $ file name”) GO fputs ($file, “$count[0]”) GO fclose ($ file) GO echo$ count [0]GO ?>
Generally the file name variable usually hold the name of the text-file with hit count in it. 
Each time you happen to add the script to the web page, it is important that you make sure you as well add a test file referred to as hits.txt’. However, it should only be with the digit “0” in it specifically to the directory whereby your web page is held. 
The $ count variable normally accesses the values with the file’ performance. This performance is meant to read the whole file into an array. Typically, in such a case the array is supposed to have only one value. Eventually, the line count {0}+ +,’then increases the value by 1. The file then is opened with fopen’ function. Note that the w’ in that specific function usually allows the files to have write accesses. Basically, if the file fails to be there, the text will be returned cannot locate hits.txt’ and then the script ends. However, if the file happens to be successfully opened, the fput’ function is supposed to write the new $ count value to your file. Your file will then be closed with fclose’ function. 
The last line of script, echo$ count {0,’ will write the hit count to your screen. However, note that in case you do not need the hit count visible, either you can live out that line, or comment it out simply by replacing this symbol i.e. # at the start of the line. 
Embedding your Code
Try saving your code as follows i.e. /’hits.php/’ and then place in the a similar directory as that of your webpage. If for instance, the web server you have is Linux server, ensure that both the /’hits.php/’ is executable and /’hitys.txt/’ file is writable by world by typing the command /’chmod 755 hits.php/’
Last but not the least; you can then test the code simply by reloading your page. Note that if the counter happens to increase, it will mean that you’ve set each and everything correctly. However, if it fails to work, check the code for typos and ensure there’s a semicolon at each of the line’s end. If you want to know more about A PHP tutorial + code kindly ensure that you carefully go through the aforementioned write up. Thank you.

1 comment: