Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create an visitor counter with PHP

In this article we will see how we can create a visitor counter for our website using PHP and a simple text file.

With the advancement in the field of computer science and technology web-development has never lagged behind, it is rapidly progressing. When we just surf through internet we would come to know that there are many website that keep history of their visitors for the record and publicity purposes. It is always fascinating to know how many people have visited your website and it gives you some sort of figure disclosing the speed with which your website is progressing and becoming more and more famous among the people.

Besides this simple counter there are companies that provide many advanced and sophisticated tools to provide different sort of information about the visitors of your website that are used for various purposes. But most often we just only need to know the number of visitors of a website for this purpose let have a look at this tutorial which will enable you to make your own counter within a few minutes.

We will be making a simple Counter for a website using flat text file without using any database. We will just require a file to keep our counter value maintained.

Make a simple php file with name index.php containing the following code or you can take any of php file that you have already made before.

Listing 1: index.php

<!DOCTYPE html >
<head>
<title> My website - MrBool tutorial </title>
</head>
<body bgcolor = "#99CC99" >
<h1> this is my website </h1> 
</body>
</html>

Now make new file named as counter.txt and save it in the same folder or directory containing index.php.

Listing 2: Php code

<?php 

$handle = fopen("counter.txt", "r");
if(!$handle){
	
 echo "could not open the file" ;

}
else {
	
	
	$counter = (int ) fread($handle,20);
	fclose ($handle);
	$counter++;
	echo" <strong> you are visitor no ". $counter . " </strong> " ;
$handle = fopen("counter.txt", "w" );
fwrite($handle,$counter) ;
fclose ($handle) ;
	}
?>

Now before understanding this code we just need a little concept of filling in php over here.

For this reason if you do not have concepts of filling the have look at the proceeding explanation which will make your basic understanding of filling in php clearer.

When dealing with files in PHP we follow three sinple and easy steps:

  • Open a file and make a file handle variable.
  • Using the the file handel variable read or write into the file.
  • Close the file using file handel variable

Keep in mind these step while reading the below paragraphs.

Fopen() function in php:

fopen () function in php is used to open a file . it takes three argument in which one is optional we only discuss here the two argument or parameters.

fopen(“file_name”, “mode”);
First argument specifies the file name to be opened and the second argument specifies the mode in which the file is to be opened that is reading mode or writing etc.
  • r = read mode i.e reading from a file.
  • w = write mode i.e writing to a file .
  • r+ = read and write mode
  • w+ = write and read mode
  • a = appending i.e when we write once to file, the second time data written is appended with data written at first time
  • a+ = reading and appending

The code above contain fopen() function having file name as counter.txt and r for read mod.

$handle = fopen ("counter.txt", "r");

The $handle is called handle variable which is used to identify the file, now whenever we want to write or read a file we will use the $handle variable. Secondly we have line of code: If (! $handle)

This statement checks weather the file is open or not if the file is not opened the code will give a message saying could not open the file. The “!” is used for not sign .this code says if if() returns false value then display the message otherwise execute the else section of the program.

In else section we simply read the file counter.txt with help of file handle variable .

The code is given below :

$counter = (int ) fread ($handle,20);

fread() function

This function is used to read data from a file. It takes two arguments the first argument is the file handle variable and second argument is length if byte that is read. In the above code the twenty byte data is read and it is converted into integer (1,2,3,4,.... these numbers are called integers) and stored into variable $counter.

fclose() function

fclose ($handle);

The line of code above uses a PHP fclose() function to close a file when we are finished with a file. This function only takes file handle variable as argument and closes the file .

Next line code:

    $counter++;
	echo" <strong> you are visitor no ". $counter . " </strong> " ;

Here when we get value in counter variable it is then incremented and the displayed on the screen .

The “.” In the code is used for concatenation that is like combining strings. The string you are visitor no is concatenated with integer number contained in counter variable .

<strong> </strong> is a html tag that is embedded in echo to show the text strong on the screen.

Looking at the next line of code:

$handle = fopen("counter.txt", "w" );

Now again we open the file with different mode this time in order to write into the file and save the incremented value to file for the next retrieval of counter variable.

Again we make file handle in order to use write() and fclose() functions.

This time mode of file w = write mode .

The next line of code will write the incremented value to file and then closes the file using fclose function taking handle as argument.

fwrite($handle, $counter) ;
fclose ($handle) ;

Writing php code into index.php show below:

Listing 3:index.php

 
<!DOCTYPE html >
<head >

<title>My website</title>
</head>

<body bgcolor = "#99CC99">
<?php 
$handle = fopen("counter.txt", "r");
if(!$handle){
 echo "could not open the file" ;
}
else {
	$counter = ( int ) fread ($handle,20) ;
	fclose ($handle) ;
	$counter++ ;
	echo" <strong> you are visitor no ".  $counter . " < /strong >  " ;
$handle =  fopen("counter.txt", "w" ) ;
fwrite($handle,$counter) ;
fclose ($handle) ;
	}
?>
<h1>this is my website</h1>
 </body>
</html >

The result of code is shown below in the figure.

Result of Listing 3.

Figure 1: Result of Listing 3.

Now just by refreshing your page you will notice that value shown to be 1 is incremented to 2.

And by just using few techniques you can place this counter values anywhere on the page you want.

One simple way is to make a div and make its position absolute and give it proper width and height.

The just by dragging it with muse you can place it anywhere on your webpage.

And if you are having some problem regarding z-index value the just assign the counter higher value so that it remain always on the top and id always displayed.

The given code with using div tag is shown below:

Listing 4: Show div tag.

<div style="width:207px; height:33px; position:absolute; z-index:999; top: 4px; left: 772px;">
<?php 

$handle = fopen("counter.txt", "r");
if(!$handle){
	
 echo "could not open the file";
}
else {
	
	
	$counter=(int )fread($handle,20);
	fclose($handle);
	$counter++;
	echo" <strong> you are visitor no ". $counter  . " </strong > " ;
$handle= fopen("counter.txt", "w" ) ;
fwrite($handle,$counter) ;
fclose ($handle) ;
	}
?>
</div>

Here we have just used inline css to adjust our counter to our will so that we can place it anywhere.

Using inline CSS result

Figure 2: Using inline CSS result

Note for the above code to run you should have installed a local host sever at your computer such as Apache server which is also available in XAMPP.



computer science Graduate :Ceo at www.gigsclerk.com (Best Marketplace to Buy and sell service online from 2$-100$ ) and www.trustingeeks.com. Interested in programming, online buissiness. Android development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login