Tuesday, 8 April 2014

Creating a Hit Counter In Php

Visitors Counter
Creating a Non Unique Hit Counter. I means By non-unique hit counter i mean that every time the page refreshed or rather every time. We call count function because we dealing lot of function in here. We gonna update a text file with a value and that value is increment on to the value which is previously on the text file. So, We have to create a file we creating with 0 and saving it naming count.txt. It will Increment depending how many times page is refreshed.

hit counter in php
Screenshot1


Code For Creating a Non- Unique Hit Counter

Count.php(Screenshot2)

counter in php
Screenshot2


<?php



function hit_count() {

$filename = 'count.txt';



$handle = fopen($filename, 'r');

$current = fread($handle,filesize($filename));

fclose($handle);



$current_inc = $current + 1;



$handle = fopen($filename,'w');

fwrite($handle, $current_inc);

fclose($handle);



}



?>

index.php (Screenshot3)

index php file
index.php (Screenshot3)

<?php



include 'count.php';



hit_count();



?>

Output

hit counter in php
Screenshot4
count in php
Screenshot5

functions in php
Screenshot6

Include File Inside Php. Always use include files in your website the reason for this copy and paste the code over for every page you want to count on so, better to include file for every time in every page we can use include function.

Creating Count.php Its going to contain the function we need to increment a count So, starting with creating function that's going to be called hit_count() function. So, what we need inside the function first of all we need count.txt so i had created $handle = fopen('$filename','r'); and we are opening to readin so we using r to specify we are reading from this file. $current = fread($handle, filesize($filename)); Line for code use fread function for reading file and Filesize function read file.

Then we are incrementing the file count.txt value with +1 and again saving with increment value to count.txt. So, again with help of file handling writing in php files we can increment value. Every time we refresh page it will increment the value and we will call value by function index.php hit_count() function.

Its every time we refresh the page its increment the value of count.txt by +1 and count the hit counts of the page. This creating a non-Unique Hit Counter.

Creating a File Based Unique Hit Counter


By Unique Hit Counter i mean same as a non-unique hit counter how ever we going to be recording the user ip address and by recording users ip address we gonna lot of mostly accurate figure because ip address always change but we can get lot more accurate figure as the how many unique visitors can take out from our website. From Non-unique hit counter we had file index.php , count.php and count.txt file we also gonna include new file ip.txt we gonna had ip address in here the users had have visited the website that gonna be stored every time they visit. So, the first thing we gonna do is we gonna count a hit that we gonna take out ip address and we are going to store in ip.txt file in line by line bases. 

Code File Based Unique Hit Counter

Count.php


<?php



$ip_address = $_SERVER['REMOTE_ADDR'];



function hit_count() {



ip_file = file('ip.txt');

foreach($ip_file as $ip) {

$ip_single = trim($ip);

if($ip_address==$ip_single){

$found = true;

break;

}else{

$found = false;





 }

if ($found == false) {

$filename = 'count.txt';

$handle = fopen($filename, 'r');

$current = fread($handle,filesize($filename));

fclose($handle);



$current_inc = $current + 1;



$handle = fopen($filename,'w');

fwrite($handle, $current_inc);

fclose($handle);



$handle = fopen('ip.txt', 'a');

fwrite($handle, $ip_address."\n");

fclose($handle);



}



}



?>

index.php (Screenshot3)

functions in php
Screenshot3

<?php



include 'count.php';



hit_count();



?>

Output

In Tutorials Series I had Better way to get visitors Ip address in php as well as before getting visitors ip address we need to check whether it is already there into the file therefore we need file handling functions. File handling checking file exits in php and file handling listing files in php. It allows us to take each ip address in each line and carefully check each one on each line. So, Its very very easy way to doing things. So, First thing we gonna do inside count.php we will make function called function hit_count() and it will be called from index.php (Screenshot3) every time. So, we had a freedom we are including this count by include function include 'count.php' then we had freedom to call function hit_count();. So, we will make now function hit_count inside count.php.

count.php firstly i will get visitors ip address. I am using simple way to grape user ip address $ip_address = $_SERVER['REMOTE_ADDR']; REMOTE_ADDR is predefined variable. So, this is environmental information about the user. Server Variable Remote Address in php and Better way to get visitors ip address in php are post which is very nice to grape visitors please read post and get better understand about how to properly detect users ip address. I am using REMOTE_ADD but i am recommended to you to look up at better way to grape users ip address.

Now Inside hit_count function first we need to Check that ip address is already there in ip.txt file or not the user had been unique visitor or regular visitor.

$ip_file = file('ip.txt')

Make an array of each line of the ip address will  deal as array. Now we using foreach statement to grape each ip address in that so we get each element of the array.

foreach($ip_file as ip);
$ip_single = trim($ip);

We will use $ip_single variable to check whether the  users ip address corresponded to one already here.

if($ip_address==$ip_single){
$found = true;
break;
}else{
$found = false;

}
  }
This is condition to check whether this ip address  is already inside file ip.txt.

If ip address will not found in the file
if ($found == false) {
$filename = 'count.txt';
$handle = fopen($filename, 'r');
$current = fread($handle,filesize($filename));
fclose($handle);

$current_inc = $current + 1;

$handle = fopen($filename,'w');
fwrite($handle, $current_inc);
fclose($handle);

$handle = fopen('ip.txt', 'a');
fwrite($handle, $ip_address."\n");
fclose($handle);

}

}

It will append the ip address of the user. and also increment the count function for count.txt. Its a simple way to create unique hit counter as well as non-unique hit counter.




No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates