Forum Moderators: coopster

Message Too Old, No Replies

most popular pages...how to tackle this?

         

dmmh

9:23 am on Mar 27, 2005 (gmt 0)

10+ Year Member



basically I am updating a column in some tables which holds a value about how many times a page was requested so I can monitor which pages are most popular

it works fine, but is very prone to cheating, because I dont know of a good way to limit this per user/ visitor

now, it will only update for logged in users, regardless of how many times they viewed the page, so if someone presses F5 100 times, it will increase with 100, which isnt very pretty.

Id like to be able to limit the number of times the field is updated to once per session, but Id like the same for not logged in users, which is where I see problems.
Because someone wont be logged in when they find me via a search engine, I cant use sessions for this...or can I?

Id like to avoid using cookies as much as possible, I store most data in tables :)

any suggestions?

wrightee

12:00 am on Mar 28, 2005 (gmt 0)

10+ Year Member



All kinds of ways depending on how lazy you are / busy your site is:

$_SESSION[$_SERVER['PHP_SELF']]=true;
if(!$_SESSION[$_SERVER['PHP_SELF']]){increasePageCount();}

is my personal laziest way to do this.

Or, if more adventurous:

1: Make table with unique key constructed of ip (IP address),page_id (some page identifier),day_hour (combination of day of month and hour)

2: Using your favorite db access object, do something like this.. if it succeeds that means that there was no unique combination for that ip,page_id and day_hour and affectedRows will contain 1. If it fails, that insert had already been run before, so that user had visited in that hour.

$db->query("INSERT IGNORE INTO page_track(ip,page_id,day_hour) VALUES ('$ip','$page_id','$day_hour')";
if($db->affectedRows()){increasePageCount();}

Of course, if they hit at 11:59 and 12:01, that'll be 2 hits. If you want more granularity, change the period, or change the method to run selects first etc etc. Doing insert ignore just saves time and db load.