Jump to content

How do I fix this...?


Bean Boy

Recommended Posts

I'm writing a program that counts page views, kind of like YouTube. But the problem I'm having is that it counts a single user multiple times.

 

How do I fix this?

 

Right now my code is something like this...

 


if($_SESSION['user'] == false){
     mysql_query("UPDATE SET page
     views=views+1
     WHERE id=1")
     or die(mysql_error());
     $_SESSION['user'] = true;
}

 

It seems that a single user often has multiple ip addresses, and this is the reason for the code counting too many times.

 

Any suggestions?

Link to comment
Share on other sites

well a couple things you can do.

1. ) If the client is not logged in as a user, make a session that is unique like their ip address plus a current time stamp.  so that way whatever they do while under guest they can be logged.

Table Views

vID int(11) primary auto

pageID int(11)

user varchar(100)

every time a user views it insert a new row into the table

but before you update the views check to see if the user has already viewed this page by taking that new session created for all guest and matching it up with the pageid of the page they are viewing and the user row

Link to comment
Share on other sites

Here create a new mysql table


CREATE TABLE IF NOT EXISTS `pageviews` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pageid` int(11) NOT NULL,
  `user` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

[/Code]

Here is some php code that could help with your problem

[Code]
<?php
//top of your page
session_start();
if (!isset($_SESSION['user']) || !isset($_SESSION['guest'])) {
    $ip = $_SERVER['REMOTE_ADDR'];
    $time = date("Y-m-d H:i:s");
    $guest = "{$ip}.{$time}";
    $_SESSION['guest'] = $guest;
}
//define page id somehow
$pageid = '';
//now for you page update
//first check to see if they have view it or not
$User = (isset($_SESSION['user']) ? $_SESSION['user'] : $_SESSION['guest']);
$Sql = "SELECT * FROM pageviews WHERE user = '{$User}' AND pageid = '{$pageid}' ";
$Query = mysql_query($Sql);
$Num = mysql_num_rows($Query);
if ($Num < 1) {
mysql_query("UPDATE SET page
    views=views+1
    WHERE id=1")
or die(mysql_error());
}
?>
[/Code]

Link to comment
Share on other sites

Do you know how sites like YouTube or even Google Analytics does it? Surely, there must be a way that's fairly accurate.

 

Not really. You can have a single user that uses multiple IPs (work/home or gets a different IP from his provider) or you have have multiple users behind the same IP using NAT. So, using the IP address is not guaranteed. Using cookie is a little better, IMHO. But, you would get multiple hits if the same user views the same content on different machines. Also, a user could delete their cookies and that would incur an additional hit if they viewed the same content after deleting the cookie. And then there are those that set their browsers to not accept cookies which would generate a hit on every view since you wouldn't be able to set a cookie to exclude them in the future.

 

As scootstah a login system would probably be the most reliable, but even that isn't foolproof. A user could create another account and view the same content again, which would trigger another hit.

 

It all depends on exactly what you ar trying to ascertain from the data that should guide you in deciding the best method.

Link to comment
Share on other sites

Do you know how sites like YouTube or even Google Analytics does it? Surely, there must be a way that's fairly accurate.

 

Not really. You can have a single user that uses multiple IPs (work/home or gets a different IP from his provider) or you have have multiple users behind the same IP using NAT. So, using the IP address is not guaranteed. Using cookie is a little better, IMHO. But, you would get multiple hits if the same user views the same content on different machines. Also, a user could delete their cookies and that would incur an additional hit if they viewed the same content after deleting the cookie. And then there are those that set their browsers to not accept cookies which would generate a hit on every view since you wouldn't be able to set a cookie to exclude them in the future.

 

As scootstah a login system would probably be the most reliable, but even that isn't foolproof. A user could create another account and view the same content again, which would trigger another hit.

 

It all depends on exactly what you ar trying to ascertain from the data that should guide you in deciding the best method.

 

Couldn't you use the ip, if you give each person a unique session attached with with ip?

Link to comment
Share on other sites

Couldn't you use the ip, if you give each person a unique session attached with with ip?

 

The session is basically the same as setting a cookie.  Including the IP as a limiter isn't going to do much for you.  As mentioned, IP's suffer from a sharing/changing problem, eg

 

- Some users share IPs via nat, common in offices and home networks.

- In some larger corporate environments, or people using an anonymizer, outbound traffic might be sent through proxies/load balanced which means requests might come from different IPs.  For example, the main page request might come from 1.2.3.4 while the request for a resource (such as video file) might come from 1.2.3.5.

 

There's no way to really account for all these (and maybe more) possible setups and do a unique count.  The best you can do is require the user to verify themselves via a login, or try and tag the user with a cookie and hope it sticks.

 

Link to comment
Share on other sites

Thanks for the responses, guys.

 

For my code, a user account wouldn't make sense.

 

I've tried several different things, like trying cookies instead of sessions, inserting the date into a table, etc.

 

kicken, you seem to understand my problem exactly! When inserting their ip addresses into a table (like someone suggested), I noticed there will be several with similar ips, which I assume is actually the same person.

 

For example:

 

1.2.3.89

1.2.3.56

1.2.3.7

 

So I'll get 3 hits (often more) for one user viewing the page once.

 

Is there any way around this problem without user accounts? I don't need 100% accuracy (which I reckon is next to impossible), but one user counting 4-8 is not reliable enough.

 

Again, how do sites like YouTube deal with this problem? I assume their views are fairly accurate.

 

Certainly, there must be a way.

Link to comment
Share on other sites

Is there any way around this problem without user accounts?

 

As we've already stated, you set a cookie and hope it works.  If the user has cookies disabled or rejects them then they will be counted each time and there is nothing you can do about that.  If they accept the cookie then you do not count them again if the cookie is present.

 

 

Link to comment
Share on other sites

Again, how do sites like YouTube deal with this problem? I assume their views are fairly accurate.

 

I assume they are logging by IP, since two different browsers don't increase the view.

 

Certainly, there must be a way.

 

Yep, log by IP or with a cookie.

Link to comment
Share on other sites

Again, how do sites like YouTube deal with this problem? I assume their views are fairly accurate.

 

I assume they are logging by IP, since two different browsers don't increase the view.

 

Certainly, there must be a way.

 

Yep, log by IP or with a cookie.

More for the log by IP though, eh? ;)

 

Just in case they get clever and keep clearing their cookies.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.