Learn To Create Your Own PHP Page Hit Counter

Posted by in Articles, PHP, Programming

Today, I will show you how you can use php’s file handling functions to create a web site page visit counter.
The script that we are going to create will open a file when called and read the value of the file then increment the value by one and then write that value to the file. We’re going to put the actual code inside a function so that way you can call the script on your page with only one line of code.

Now let’s begin. In order to open a text file in php, we need to call the function fopen(). This will open the file, if it exists, and return a file pointer. Instead of using the file name, you pass a file pointer to functions. Here is the beginning of our code:

<?php
function count_hit()
{
$file_pointer= fopen("hit.txt", "r+");

In the above script, we declared a function by using the keyword function followed by the name of our function. Next, we used fopen() passing the name of a file in the same directory as the script where the page visits data will be stored as the first parameter; we then passed the second parameter r+, this tells the server that we want to open the hit.txt file for reading and writing. For more details about text file handling parameters, please review my previous tutorial about PHP and txt file interaction.
We must create a blank text file named as hit.txt in the directory where the current script is situated.
fopen() can take one more parameter: 1. If you pass the third parameter as 1, the file you passed the name of in the first parameter will be searched for in the include path. We store the result of fopen() in a variable named $file_pointer. If the file does not exist, fopen() will return false. Therefore we’ll need hit.txt initially.

if ($file_pointer == false)
{
return "Error:File not found or could not be opened!";
exit;
}

Now since file is opened, we’ll need to read the file, to do so, we’ll use function fread().

$hit= fread($file_pointer, filesize("hit.txt"));

The syntax for fread is (file open, filesize). To get the file open we’ll be using $file_pointer string and to get the size of the file we use the filesize(“filename.txt”) function. Now the contents of the file are stored in a variable with the name $hit. We will use the trim() function to strip any whitespace from the beginning and ending of the string.

$hit= trim($hit);

Trim will remove all the blank white spaces if exists, this will be helpful to maintain the file size and unexpected results. Now we have read the contents of the file into a variable and trimed up the variable, we will increase it’s value by 1 per hit, and write it back into the file. Before we write to the file, we need to go back to the begining of the file so we can write over the current count. We do this by using fseek().

$his++;
fseek($file_pointer, 0);
$result= fwrite($file_pointer, $hit);
if ($result == false)
{
return "Error: could unable to write file!";
exit;
}
else {
return $hit;
}

We use the increment operator (++) to add 1 to the hit count, then we use fwrite() function to write to the file. We then pass the file pointer and the hit count to fwrite(). If it returns false, the it will show error message. Lastly, we return the value of $hit, which holds the hit count.
Now we’ll need to close the file after reading and writing it. To do that we use fclose() and pass it the file pointer, and also we’ll add some error checking.

$close= fclose($file_pointer);
if ($close == false)
{
return "Error: could not close the file!";
exit;
}

And that’s all, we’re done! Here is the complete script for a hit counter:

<?php

function count_hit()

{

$file_pointer= fopen("hits.txt", "r+");

if ($file_pointer == false)

{

return "Error: could not open the file! It may not exist!";

exit;

}

$hits= fread($file_pointer, filesize("hits.txt"));

$hits= trim($hits);

$hits++;

fseek($file_pointer, 0);

$result= fwrite($file_pointer, $hits);

if ($result == false)

{

return "Error: could not write to the file!";

exit;

}

else {

return $hits;

}

$close= fclose($file_pointer);

if ($close == false)

{

echo "Error: could not close the file!";

exit;

}

}

?>

Now to display the hit on a webpage by adding following code snippet:

<?php echo count_hit( ); ?>