Go Back   Cloud Computing > Support > PHP Forum
 

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-11, 19:26
BOD Member
 
Join Date: Mar 2011
Posts: 14
Default Unique Hits Counter

This tutorial is simple, but very effective. It will count the unique hits per day and keep a permanent record. I will first explain how it works, and then I will go ahead and give you the code.
First we set up a database and put a chunk of code at the top of every page. This code will catch the users IP and write it to the database along with the date. However, when it goes to write the IP, it first searches for the IP to see if it has already visited the site, but it will only search that day. If the IP has already visited that day, it will not record it again. For example, if IP 123.456 visited on 02/04/00 it will record it, then if that IP visits again on 04/04/00 it will again record it, but if the visit twice on 02/04/00 it will not record the second time.
Let’s set up the database. Run this SQL script on whatever database you want, its not particularly large.

Code:
CREATE TABLE `uniquehits` (
`ip` varchar(255) NOT NULL,
`date` date NOT NULL default ’00-00-0000’
);
Now put this code at the top of every page you wish to record the hits from. This tutorial will not record hits for individual pages but will consider every page as a whole. So if they visit 2 pages that have the code in the same day it will record the IP once.

Code:
<?php
$connection = mysql_connect ('localhost', 'USERNAME', 'PASSWORD')
or die ('Unable to connect!');
mysql_select_db('admin1_counter') or die (mysql_error());
$_SERVER['REMOTE_ADDR'];
$ip = $_SERVER['REMOTE_ADDR'];
$fetch = mysql_query("SELECT * FROM uniquehits WHERE ip ='".$ip."' AND date=NOW()") or die(mysql_error());
if ( mysql_num_rows($fetch) == 0 )
{
mysql_query("INSERT INTO uniquehits(ip, date) VALUES('$ip', NOW())") or die(mysql_error());
}
mysql_close($connection);
?>
Ok, now we need to display the amount of hits. I will give you 2 options, one is displaying the hits from that day. That means it will go back to 0 everyday or you can display total unique hits recorded, not including double IP records on separate days. So in other words it will display the amount of IP’s that have viewed your site since you installed the script.
This code will display the daily hits.

Code:
<?php
$connection = mysql_connect ('localhost', 'USERNAME', 'PASSWORD')
or die ('Unable to connect!');
mysql_select_db('admin1_counter') or die (mysql_error());
$count = mysql_query("SELECT * FROM uniquehits WHERE date=NOW()") or die(mysql_error());
$unique = mysql_num_rows($count);
echo $unique;
mysql_close($connection);
?>
This code will display the total IP’s.

Code:
<?php
$connection = mysql_connect ('localhost', 'USERNAME', 'PASSWORD')
or die ('Unable to connect!');
mysql_select_db('admin1_counter') or die (mysql_error());
$counttotal = mysql_query("SELECT DISTINCT ip FROM uniquehits") or die(mysql_error());
$uniquetotal = mysql_num_rows($counttotal);
echo $uniquetotal;
mysql_close($connection);
?>
If you know MySQL feel free to play around with this and display results as you wish, eg by month, year, etc.
Hope you enjoyed this tutorial, pretty simple, but effective.


Thanx
Reply With Quote
  #2 (permalink)  
Old 05-09-11, 00:46
BOD Member
 
Join Date: Nov 2010
Posts: 104
Default

Hi

Thank you for this great and valuable tutorial.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT -6. The time now is 01:08.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 1999-2012, BODHost Ltd. All rights reserved.