Bean Boy Posted March 30, 2012 Share Posted March 30, 2012 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? Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 30, 2012 Share Posted March 30, 2012 There's no sure-fire way to tell if a request is unique or not. You can either check by IP or set a cookie or something. Both are not very reliable, but it's the best you can do aside from a login system. Quote Link to comment Share on other sites More sharing options...
Bean Boy Posted March 30, 2012 Author Share Posted March 30, 2012 Do you know how sites like YouTube or even Google Analytics does it? Surely, there must be a way that's fairly accurate. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted March 30, 2012 Share Posted March 30, 2012 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 Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted March 30, 2012 Share Posted March 30, 2012 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] Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 30, 2012 Share Posted March 30, 2012 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. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted March 30, 2012 Share Posted March 30, 2012 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? Quote Link to comment Share on other sites More sharing options...
kicken Posted March 30, 2012 Share Posted March 30, 2012 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. Quote Link to comment Share on other sites More sharing options...
Bean Boy Posted March 31, 2012 Author Share Posted March 31, 2012 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. Quote Link to comment Share on other sites More sharing options...
kicken Posted March 31, 2012 Share Posted March 31, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 31, 2012 Share Posted March 31, 2012 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. Quote Link to comment Share on other sites More sharing options...
NLT Posted April 2, 2012 Share Posted April 2, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.