Unique Hit Counter using PHP/MySQL

<?php
if (getenv(‘HTTP_X_FORWARDED_FOR’))
{
$domain=getenv(‘HTTP_X_FORWARDED_FOR’);
}
else
{
$domain=getenv(‘REMOTE_ADDR’);
}
// $domain = GetHostByName($REMOTE_ADDR);
$d1=date(“Ymd”);
$connect = mysql_connect(“localhost”, “samreno_samren”, “samren”) or die (“Check your server connection.”);
mysql_select_db (“samreno_samren”);
//Check the IP and Date.
$results=mysql_query ( “SELECT * FROM hit_counter WHERE iIP=’$domain’ and dDate=’$d1′” );
$num = mysql_num_rows ($results);
if ( $num > 0 )
{
//echo “The IP  is already assigned.<br>”;
//exit;
}
else
{
$insert=”INSERT INTO hit_counter (iID,iIP,dDate) VALUES (”,’$domain’,’$d1′)”;
$results = mysql_query($insert) or die(mysql_error());
}
//Curdate visitor.
$query=”select count(*) from hit_counter where dDate=’$d1′”;
$result=mysql_query($query) or die(mysql_error());
$querydata=mysql_fetch_row($result);
$tot=$querydata[0];
echo “<strong>T</strong>oday’s total visitors: $tot. <br>”;
//Total visit
$query=”select count(*) from hit_counter”;
$result=mysql_query($query) or die(mysql_error());
$querydata=mysql_fetch_row($result);
$tot=$querydata[0];
echo “<strong>Y</strong>ou are visitor no: $tot. <br>”;
?>

4 thoughts on “Unique Hit Counter using PHP/MySQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s