Wednesday, December 3, 2014

Simple Hit Counter Tutorial in PHP

A Hit counter is a simple script which considers the number of times web pages have been accessed. It provides the information to the visitors and owner that how the page is popular. It can be created in PHP with using only nine lines of code.

PHP - Hit Counter Tutorial
It only counts the total number of times the web page has been accessed, but it does not count the number of unique visitors of the page.

Basics :-

Basically, PHP HIT counter is meant to simply update the text file, which often keep the track of the number of times the page has been accessed. On the bottom, the PHP script is to read the current no show in the file. Then it adds 1 digit to the number then writes to the file the number and at last closes the file.

Now, The script is embedded into the html code of the page in order to easily executed each time the web page is accessed.

The Script :-

The simple hit counter script looks like below :-

>?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 ?>

Every time you need to add the script to the web page, it is necessary to make sure that you also 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 accesses the values of the file performance. This performance is meant to read the whole file into an array. In such a case, the array is supposed to have only one value. Eventually, The line count {0}++ then increases the value by 1. Then the file will open with fopen' function. Kindly remember that w' in that specific function usually allows the files to have write accesses. If the file fails to be there, the text will be returned cannot locate hits.txt and then the script gets ends. On another hand, if the file 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.

Echo$ count {0' will write the hit counter to your screen. However, if you do not need the hit counter visible, then you should live out that line, or comment it by replacing. # symbol at the start of the line.

Embedding your Code :-

Try to save your code as follows : /’hits.php/’ and then place it in the similar directory as same as your web page. If your web server is Linux, then make sure that both the /’hits.php/’ is executable and /’hitys.txt/’ file is writable by the world by typing the command /’chmod 755 hits.php/’

At last, you can test the code by reloading the page. Note that if the counter happens to increase, it means that you have set everything true. On another hand, If it fails to work, check the code for typos' error and be sure there is a semicolon to the end of each lines. As a PHP Developer, It would be very helpful in your PHP Application Development.

Reference :- http://www.thecodertips.com/2014/02/easy-php-hit-counter-tutorial.html

0 comments:

Post a Comment