Jul
4th

Simple PHP Visitor Hit Counter

Files under PHP & MySQL

This week, I will provide this tutorial that will guide you on how to make a simple PHP hit counter. This is great as you will be able to show your visitors how many times your web page has been loaded.

Tutorial On Creating PHP Visitor Hit Counter

Step 1: Create a script called ‘visitorhit.php‘ and a text file called ‘counter.text‘ with a value of 0 and save it to the same directory as your php file.

visitor hit

hitcounter

Note: Make sure that your text file has enough permissions, so set the permission CHMOD to 777.

Step 2: Start your PHP coding now, open your ‘visitorhit.php’ and set the variable for the file named ‘counter.text’ and place this between your PHP tags.

Note: Remember to place the filename in quotes and make sure that the semicolon is placed on the end of the line.

<?php
$number_of_visit = (”counter.text”);
?>

Step 3: Then you need to have the file to open it and tell PHP to read the number inside. Set the number of visits ($hits) to the current value of the counter.text file.

<?php
$number_of_visit = (”counter.text”);
$hits = file($number_of_visit);
?>

Step 4: Then, it must increase the value by 1 for the current hit when the page is loaded or refreshed. Thus, add 1 to your current value of $hits. $hits should now be whatever’s inside the file “counter.text” plus one. It means that if inside your text file is 0, then $hits should equal 0 + 1 = 1.

<?php
$number_of_visit = (”counter.text”);
$hits = file($number_of_visit);
$hits[0] ++; // $hits = $hits + 1;
?>

Step 5: Open the file that is keeping count so we can write to it. Write the function fopen to open the file “counter.text” in the first parameter. The “w” tells PHP that we are only opening the file to write. It also tells PHP to delete everything in the file and set the pointer to the beginning.

<?php
$number_of_visit = (”counter.text”);
$hits = file($number_of_visit);
$hits[0] ++;
$fpointer = fopen($number_of_visit, “w“);
?>

Step 6: Replace the value in the file with the new $hits number after it was incremented. So, you will get the new counter value.

<?php
$number_of_visit = (”counter.text”);
$hits = file($number_of_visit);
$hits[0] ++;
$fpointer = fopen($number_of_visit, “w“);
fputs($fpointer , “$hits[0]“);
?>

Step 7: Finally, you must close the connection to counter.text

<?php
$number_of_visit = (”counter.text”);
$hits = file($number_of_visit);
$hits[0] ++;
$fpointer = fopen($number_of_visit, “w“);
fputs($fpointer, “$hits[0]“);
fclose($fp);
?>

Step 8: Set the code to display your hit number using echo command. This is to print the new counter value to the screen.

<?php
$number_of_visit = (”counter.text”);
$hits = file($number_of_visit);
$hits[0] ++;
$fpointer = fopen($number_of_visit, “w“);
fputs($fpointer, “$hits[0]“);
fclose($fpointer);
echo $hits[0];

?>

Step 9: To use this script you just have to add a PHP statement that calls that PHP file. Use the following include:

You can add this code to anywhere you like to put it. But this is just for your testing only because if all people is using this file and then they will share the same visitor hit counter as well. If you want to have own hit counter, please contact me at info@imDavidLee.com

<?php
include(“http://codebasic.net/download/visitorhit.php”);
?>

Run and test with it!!! Have a nice day!!!



One Response to “Simple PHP Visitor Hit Counter”

  1. By ravi ahar on Sep 1, 2009 | Reply

    dear,
    i’ve already used this type of counter but i always failed after sometime. the counter is reseted to blank & gave error.
    you can see similar counter in smsonline123.com, jansattaraipur.com, specialblasts.com, powersolengineers.com
    it is not fully trustable, so i’m going to use mysql db for this purpose

Post a Comment