hit counter to count unique hits only

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

hit counter to count unique hits only

Post by tomsace »

Hey,
I have just got my hit counter working storing the hits into mysql.
The problem is it is counting every time the page is refreshed etc... I just want it to show unique visits.

Here is my code:

Code: Select all

<?php
$id=mysql_real_escape_string($_GET['id']);
//Adds one to the counter
mysql_query("UPDATE games SET hits = hits + 1");
 
//Retreives the current count
$count = mysql_fetch_row(mysql_query("SELECT hits FROM games WHERE id = '$id'"));
 
//Displays the count on your site
print "$count[0]";
?>
Any help apreciated.
Last edited by Benjamin on Sat Apr 25, 2009 2:33 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: hit counter to count unique hits only

Post by McInfo »

There is no way to know whether an anonymous hit is 100% unique, but there are some user attributes that you can use to narrow it down, like $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'], and cookies. If your users are logged in, it is a lot easier to identify unique hits.

Edit: This post was recovered from search engine cache.
Post Reply