Page 1 of 1

Counting the number of unique views of a page

Posted: Mon Aug 15, 2005 6:43 am
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.

Posted: Mon Aug 15, 2005 7:08 am
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

Posted: Mon Aug 15, 2005 7:36 am
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?

Posted: Mon Aug 15, 2005 7:38 am
by feyd
viewtopic.php?t=1157


I type that link too often :?

Posted: Mon Aug 15, 2005 7:53 am
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.

Posted: Mon Aug 15, 2005 10:52 am
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