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 – 2

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

DOC,PDF

Here in this part we are going to see the logic for saving the ip address of the user and to count the number of users accessed the file.
Here we need to consider the following,
-We need to check whether the ip address exists in text file (ip.txt).
-If the text file exists then we don’t need to count and also we don’t need to save the ip address as it already exists.
-So we need to skip counting and saving the ip address by using break statement.

Code:
if($ip_address == $ip_single){
echo ‘Ip address found’;
break;
}else{
// Logic for counting and saving the ip address
}

-We are using if loop to check the condition and if ip address exists, the condition will be satisfied then counting and saving ip address will be skipped as we are placing the break statement inside the loop.
-If the condition is not satisfied then we will go for counting and saving the ip address.
Here we have the logic for counting and saving the ip address as follows,

Code:
else{
echo “Not Found”;
$filename =”count.txt”;
$handle = fopen($filename,’r’);
$current = fread($handle,filesize($filename));
fclose($handle);

$current = $current + 1;
$handle = fopen($filename,’w’);
fwrite($handle,$current);
fclose($handle);

$filename = “ip.txt”;
$handle = fopen($filename,’a’);
fwrite($handle,”$ip_address.\n”);
fclose($handle);
}

Let us see the logic for counting,
-When no user accessed the file (count.php) then the value in the file count.txt is 0.
-When any user is accessed the file then the value in the count.txt will be incremented by one.
-So whenever a new user is going to access the file then the value in the file count.txt will be incremented.

$filename =”count.txt”;
$handle = fopen($filename,’r’);
$current = fread($handle,filesize($filename));
fclose($handle);

$current = $current + 1;
$handle = fopen($filename,’w’);
fwrite($handle,$current);
fclose($handle);

-First we need to open the text file using fopen() function in reading mode.
-Then we are reading the file using fread() function.
-We are saving the content of the file into the variable $current.
-Now the $current had the value of the text file.
-Later the $current variable is going to be incremented by one and the incremented value is going to be written into the file itself using fwrite() function.
-Before writing into the file we need to open the file as we closed the file earlier using fclose() function.

-So whenever a new user accesses the file count.php then the value in count.txt is going to be incremented.
Now we will see the code for saving the ip address,
-Whenever the ip address is not present in ip.txt file then ip address of the user is going to be saved in ip.txt file.
-Whenever a new user access the files then the ip address of the current user is going to be saved.

$filename = “ip.txt”;
$handle = fopen($filename,’a’);
fwrite($handle,$ip_address.”\n”);
fclose($handle);

-We can get the ip address of the user by using $_SERVER[] variable.
-we have $_SERVER[‘REMOTE_ADDR’] variable to get the ip address.
-we are assigning the ip address to the variable $ip_address so it will hold the value of ip address.
Now we have the ip address of the current user in $ip_address variable.
-Here we are opening the file ip.txt using in appending mode.
-We are writing the $ip_address value into the file ip.txt using fwrite() function.Here we are using “\n” in fwrite() function to write the ip address in a new line (as we need to retrive them as a single element in foreach loop).
Hence we got the logic for counting the number of users and saving the ip addresses of the users.
In next part we will implement the logic which we discussed above.

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

DOC,PDF

Leave a Reply