mobile

Just the other day, I was asked about how to create a website counter script. Mostly the question is for PHP, so, here, instead of directing you to sophisticated scripts - especially for beginners - we'll show you how to do that in a simple and effective way starting from scratch. To solve this problem, you'll need a place to store your data - visitors no - in some place. Then when someone visit your website, just read the value form the old data and increase by 1 then display the value and replace the value in your storage place. First of all, create a file called count.dat. Open your favorite text editor and create a new PHP class for this purpose and a method to handle the data lets call this file counter.php.

<?php 
class hitCounter {
//Set a File Name
public $fileName = "count.dat";
  public function getHitCount(){
  //Read the previous value of counter from file
  $counter = intval( trim(file_get_contents($this-&gt;fileName)) );
  $counter++;
    //Store the new value in the same file by overwriting it.
  file_put_contents($this-&gt;fileName, $counter, LOCK_EX) or die("error");
    return $counter;
   }
}

So, here in this simple hitCounter class, what we have is basically a function that read the file for any old value and increment that by one then store it back. It then return the new value. Save both the count.dat and counter.php files in the same folder. Now, to use this script - say in your home page (index.php) - all you have to do is include the file and create a new instance of the class as follows:

<?php
.
.
.
$obj = new hitCounter(); 
$visit = $obj->getHitCount(); 
echo "You're visitor no: " . $visit;

So, now open your home page and you should see that your page increase every time you refresh it. The limitation here is that if a person view your site many times a day your site will keep increasing the value, so, you'll not get a right numbers of visitors in your website. Another limitation is that if the count.dat file is not created beforehand then this script will give an error. In the next section we'll show you how to overcome that. You can use a cookie to know if the person has visited your site before. Then also, add functionality if the data storage file exist or not? To the previous class add the following: 

<?php
class hitCounter { 
 //Set a File Name
 public $fileName = "count.dat" ;
 
 //Set number of hours a visitor is considered as unique
 public $visitHour = 24;
 
 //Set a cookie name
 public $cookieName = "hitCounter"; 
 
 public function setHitCount(){
  //First Check if file not exist then create one//
  if( !file_exists($this->fileName) ) {
            file_put_contents($this->fileName, '', LOCK_EX) or die("Error: Cant find File");
        }
  //Read the previous value of counter from file
  $counter = intval( trim(file_get_contents($this->fileName)) );  
  
  //Now we check if the visitor is a returning visitor
  if( !isset($_COOKIE[$this->cookieName]) ){
   //Increase the counter
   $counter++;
   
   //Store the new value in the same file by overwriting it.
   file_put_contents($this->fileName, $counter, LOCK_EX) or die("error");
   
   //Set a cookie
   setcookie($this->cookieName, 1, time() + 60 * 60 * $this->visitHour);
  }
 }
 
 public function getHitCount(){
  return intval( trim(file_get_contents($this->fileName)) );
 }  
}
//Let's create an instance of the class
$obj = new hitCounter();
$obj->setHitCount();

Here, we've added a number of hours that a person who visit the website to be considered as a new visitor. Then a cookie name is given. A cookie is a small file that is sent to the user computer and stored there. Every time a user visit the site we can access the cookie and check if it is still exist. So here, we set the time as 24 hours before our cookie expires. You can change this to any value you want. The method setHitCount() now check first if the file exist or not, if not then it create a file. Next the method check if the cookie exist or not? If cookie is not there then the function increment the value of counter and stores it in the file. As, you can see we've added a new method getHitCount() which will just retrieve the value from the file. Now, to set cookie it is always done at the beginning of a document before any HTML content is there. To do that we've created an instance of the class and call the method setHitCount();. Also, you'll need to include this file at the top of your php document. Here, is an example of how to use this class: 

<?php require_once("counter.php"); ?>
<!DOCTYPE HTML>
<html>
	<head><title>Counter Test</title></head>
<body>
<?php
 //Write the value of new visitor;
 echo "You're Visitor No: " . $obj->getHitCount();
?>
</body>
</html>

With this simple script I hope you can enhance it more for your needs. You can also download the script from here Download Counter Script

Post Label(s): Web Design PHP - MySQL
fdiengdoh

Asst. Professor in the Department of Chemistry, St. Anthony's College. Hobbyist programmer, photographer, videographer, blogger.

Comments

No comments yet.

Leave a Comment

© 2010 - 2025 Designed By fdiengdoh.com

Privacy Policy