Quantcast
Viewing latest article 3
Browse Latest Browse All 10

PHP Hit Counter Tutorial

Image may be NSFW.
Clik here to view.
PHP Programming

 

PHP Hit Counter Tutorial : We know that mostly every website uses a Counter to Check the No. of  Unique Visitor on their Website or Blog . So in this PHP Tutorial we will design a application which help to count the unique visitor on website.

In This Application we will get No. of Visitors in a text file and handling this text by using file handling commands to get the list of unique IP. By Using IP Address we can get unique Visitor. If the same person visit the page more than one time then also this application will count it to one visitor according to it’s IP Address. That mean Count will increase when system found different IP Each time.

Before Writing this code first create a text file of name ip.txt and count.txt.

 

ip.txt : This File Store No of IP Addresses.

count.txt : This File Store No of Unique Visitors.

 

Then Use Following Code.

 

Unique Visitor Code :

<?php

function hit_count()
{
$ip_address = $_SERVER['REMOTE_ADDR'];
$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); 
}
}
?>

 

The post PHP Hit Counter Tutorial appeared first on CodingTalks.


Viewing latest article 3
Browse Latest Browse All 10

Trending Articles