Creating a Unique hit counter in PHP

Creating a Unique hit counter in PHP
Creating a Unique hit counter in PHP

Topic – 52
Creating a Unique hit counter in PHP
Part – 1

images-1-300x123-300x1234-300x123

DOC,PDF

-In last topic we have seen how to create a Non- Unique Hit counter.
-In last topic we counted that how many times a file is opened.
Even if the same user opens it then the count is going to be incremented.
-Here in this topic we are going create a unique hit counter.
-Unique hit counter means when one user accessed the file then for once it will be counted and if the same user accessed the file for second time then it should not count i.e., unique for an user.

For creating a unique hit counter we need to consider the following,
-A php file to be accessed by the users.
-A text file for counting the number of users who accessed the file.
-A text file to save the ip address of the users.
We have the files as follows,
– count.php – File to be accessed by the user
– count.txt – File which is for saving the count number
– ip.txt – File used to save the ip address.

-To know the ip address we are using $_SERVER[‘REMOTE_ADDR’] variable.
we are storing the ip address of the user into a variable.
Then we are checking if that ip address is present in the text file (ip.txt).
If the ip address is present in the text file then file accessed will not be counted.
If the ip address is not present then the file accessing will be counted and the ip address of the user will also be saved in ip.txt file .When the same user is accessing the file then ip address is going to be checked in ip.txt, so the ip address will be present in it hence the we are skipping the count when the ip address is found.

First we are going to retrieve the ip addresses saved in ip.txt,
-we have some ip addresses in ip.txt, we are echoing the ip addresses.
Program:
<?php
function hit_counter(){
$ip_file = file(‘ip.txt’);
foreach($ip_file as $ip){
$ip_single = $ip.’,’;
echo $ip_single;
}
}
hit_counter();
?>
php52-1-c
Output:
127.0.0.1, 190.25.32.1,

In above output we are getting white spaces so we need to eliminate them.
We have trim function which is used to remove the white spaces.

Program:
<?php
function hit_counter(){
$ip_file = file(‘ip.txt’);
foreach($ip_file as $ip){
$ip_single = trim($ip.)'<br>’;
echo $ip_single;
}
}
hit_counter();
?>
php52-2-c
Output:
127.0.0.1
190.25.32.1

-So we got the ip addresses from ip.txt in variable $ip_single.
-We are using for each loop to retrieve the ip address from the ip.txt file.
-This loop retrieves a single line as element. So we saved the each ip address in a new line.
-If any new user is going to access the file then their ip address should be saved in new line.

-After getting the ip address from the ip.txt file we need to compare the ip address with the current user’s ip address.
-The ip address of the current user will be obtained by the using the $_SERVER[‘REMOTE_ADDR’] variable.
-After comparing the ip address we need to put a condition using if loop.
Program:
<?php
function hit_counter(){
$ip_address = $_SERVER[‘REMOTE_ADDR’];
$ip_file = file(‘ip.txt’);
foreach($ip_file as $ip){
$ip_single = trim($ip.)'<br>’;
echo $ip_single;
if($ip_address == $ip_single){
echo “IP address Found”;
break;
}else{
echo ” IP address Not Found”;
// Here we need to write the code for counting and saving the ip address of the user.
}
}
}
hit_counter();
?>
php52-3-c
Output:
IP address Found //If the ip address is present in ip.txt file
IP address Not Found //If the ip address does not present in ip.txt file

Let us see the code for counting and saving the ip address in the next part of this topic.
In next part we are going to understand the logic and we will write the code for logic to save the ip address and also to count.

images-1-300x123-300x1234-300x123

DOC,PDF

Leave a Reply