Counting the number of unique views of a page

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
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Counting the number of unique views of a page

Post by keveh »

Hi There,

I was wondering if anybody could help me to count the amount of unique visitors to a page.

If you take a look at this page:

http://www.thewanderer.co.uk/

You can see that there is a counter for each of the articles. The way in which the count is recorded at the moment is by increasing the number every time the page is viewed, and storing it in a database.

Here is the code that is used:

Code: Select all

//Increment Number of views of the feature.
$a_row = getrow( "features", "id", $id, $link );
$inc = $a_row[views];
$inc++;
$sql = "UPDATE features SET views='$inc' WHERE id='$id'";
$result = mysql_query( $sql, $link );
if ( !$result ){
	die( "incr_views error: ".mysql_error() );
}
but if you look at the front page again, you can see that people have been abusing the count by simply refreshing the page over and over.

So I was wondering which is the best way to only count the unique amount of visitors?


Thanks a lot for any help.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

set a cookie, then check if the cookie exists and if it does then don't count the hit. also do the same thing with a session incase they have cookies disabled, third check IP addresses but the IP addresses is no good somtimes becuase like if everyone at a school goes to it, it will only be counted once becuase every computer at the school will have the same ip usually. the cookie and session are your best bet
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post by keveh »

When I try and set the cookie using:

Code: Select all

//Increment Number of views of the feature.
$a_row = getrow( "features", "id", $id, $link );
if(isset($_COOKIE['$id'])){
	$inc = $a_row[views];
	$inc++;
	$sql = "UPDATE features SET views='$inc' WHERE id='$id'";
	$result = mysql_query( $sql, $link );
	if ( !$result ){
		die( "incr_views error: ".mysql_error() );
	}
} else {
	setcookie ($id, $id, time()+86400);
}
it throws up this error:

Warning: Cannot modify header information - headers already sent by (output started at /usr/local/psa/home/vhosts/thewanderer.co.uk/httpdocs/testsite/content.php:162) in /usr/local/psa/home/vhosts/thewanderer.co.uk/httpdocs/testsite/content/feature.php on line 13


any suggestions as where i am going wrong?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=1157


I type that link too often :?
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post by keveh »

I have put the code in the right place, but for some reason it is adding 3 on to the count whenever somebody visits the page.

have my cookies been set right?

when a person clicks on an article, it passes the articles id on to another page, so it is adding this to the cookie value, and then the else statement looks if a cookie has been set for that article id.

it looks right to me, so obviously i know nothing.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

justa side note, why do this

Code: Select all

$a_row = getrow( "features", "id", $id, $link ); 
$inc = $a_row[views]; 
$inc++; 
$sql = "UPDATE features SET views='$inc' WHERE id='$id'";
when all you need is

Code: Select all

$sql = "UPDATE features SET views=views + 1 WHERE id='$id'";
or similar
Post Reply