01 October 2008 ~ 54 Comments

A Simple PHP Visitor Counter

This article is designed to help you make a very simple hit counter for your php enabled website. I will talk you through each stage and explain both what is happening and why. There are many ways that we can achieve our goal of creating a visitor counter using PHP. In this article I will show you how to create one using a text file and how to incorporate it into any page using a simple code snippet.

Requirements

  • A strong grasp of HTML is reccomended before attempting to tackle PHP.
  • A PHP enabled web server running PHP 4.x.x or higher on either a unix or Microsoft Windows platform.
  • This is a basic article, however some knowledge of PHP is desired, such as what functions are, what variables are and the basics of the syntax in general.

The Basics

The very 1st thing we need to do is decide where the text file we are going to store our visitor count is going to be on the server. You can choose to place it anywhere you like, in the example we use the path c:\htdocs and the file is called counter.txt. This means we have a full path for the file that will store our visitor count of c:\htdocs\counter.txt.

Open this file up, type in the number ‘0’ – without the quotes – save it and exit. We have now set our counter to 0, or zero. Our script is going to use this file as a reference to see how many people have visited our site and then increment it for every new visitor.

Our second decision is what are we going to call our file. The file is seperate from the pages you wish to monitor, and as discussed briefly in the Introduction we will cover how to include it in your other pages a bit later. For the purposes of the tutorial I have named our file visitorcount.php. You need to create this file, again, wherever you like, it does NOT have to be in a directory your webserver can access.

So by now we should have two files: counter.txt and visitorcount.php

The Code

The first thing we will do is define the name of the file that will record the number of visitors and reference this to a variable. This is the file counter.txt that we created earlier.

//define the file name and its path
$filename = “c:\\htdocs\\counter.txt”;

//note we escape the backslashes in the file system path
Then we need to open the file for reading so we can check the current value, and have this referenced to a variable. //open the file
$handle = fopen($filename, “rb”) or die (“Failed to open the file”);

//note we are using this on a windows system and
//thus use the “b” parameter as it is a Binary file system.
Next we need to read the current value in the file, so we can then add 1 to it to represent our latest visitor. //read the contents of the file, in our case it will be an integer
$data = fread($handle, filesize($filename)) or die (“Failed to read the file”);

//lets now add 1 to the number we read, therefore recording our new visitor
$data = $data + 1;

//note we take the variable $data and simply add one to it, reusing
//the previously defined variable for ease.
Then we close the file for now. fclose($handle);
//and close the file as we dont need it any more
The next part uses slightly different methods, the reason for this is just to show there are many ways to identical actions using PHP. It is time to write our new value to the file. We check that the file is writeable (i.e has the correct permissions set on the server). The code below is commented and speaks for itself.

//lets now write to our file.
//make sure the file exists and is writable first.
if (is_writable($filename)) {

//if it isnt then throw up an error and exit the script.
if (!$handle = fopen($filename, ‘wb’)) {
//note the useage of “wb” in fopen() ‘s mode parameter
//as we are Writing(w) to a binary file system(b).
echo “Failed to open the file”;

//this is the error message it will show if the file is not writeable
exit;
}

// If it is writeable lets write our new number to our opened file.
if (fwrite($handle, $data) === FALSE) {
echo
“Failed to write to the file”; //error and exit if failed.
exit;
}

If all was succesful and we updated the file with the new value, we can then echo out the visitor number, or hit count. If you want to keep this data private just comment the line below out.
echo
“Welcome visitor number $data”;

Then we close the file.
fclose($handle); //and close the file as we dont need it any more

} else {
This is the error we show if we can not access the file. echo “The file is not accessable.”; //error and exit if failed
exit();
}

Now we need a way to make this work on our website. To include this in any php page on your website simply include the page at the point you wish the counter to be displayed. You can format the number with HTML as normal.

include_once(“visitorcount.php”);?>

And thats it. A very simple hit counter using php and a text file. The full interupted code is below.

//define the file name and its path
$filename = “c:\\htdocs\\counter.txt”;

//note we escape the backslashes in the file system path

//open the file
$handle = fopen($filename, “rb”) or die (“Failed to open the file”);
//note we are using this on a windows system and thus use the

//”b” parameter as it is a Binary file system.

//read the contents of the file, in our case it will be an integer
$data = fread($handle, filesize($filename)) or die (“Failed to read the file”);

//lets now add 1 to the number we read, therefore recording our new visitor
$data = $data + 1;
//note we take the variable $data and simply add one to it,
//reusing the previously defined variable for ease.

fclose($handle); //and close the file as we dont need it any more

//lets now write to our file.
//make sure the file exists and is writable first.
if (is_writable($filename)) {

//if it isnt then throw up an error and exit the script.
if (!$handle = fopen($filename, ‘wb’)) {
//note the useage of “wb” in fopen() ‘s mode parameter as
//we are Writing(w) to a binary file system(b).
echo “Failed to open the file”;
exit;
}

// Write our new number to our opened file.
if (fwrite($handle, $data) === FALSE) {
echo
“Failed to write to the file”;
//error and exit if failed.
exit;
}

echo “Welcome visitor number $data”;

fclose($handle); //and close the file as we dont need it any more

} else {
echo
“The file is not accessable.”; //error and exit if failed
exit();
}

?>

//THIS IS TO BE USED ON THE PAGE THAT YOU
//WANT THE COUNTER TO WORK ON – NOT AS PART OF THE MAIN CODE!
include_once(“visitorcount.php”);
?>




54 Responses to “A Simple PHP Visitor Counter”

  1. yustasgrat 6 September 2017 at 7:54 am Permalink

    look here my site


Leave a Reply