views:

48

answers:

2

Hey guys, im building a small product catalog for a client. I am currently trying to figure out how to go about allowing the user to sort by "popularity". I figured the easiest/simplest way to do this would be by having a "views" field in each product record.

Originally I didn't think this through (duh) and thought I would just add one to the "views" count when the function to retrieve a specific product is called. Of course the problem with this is that it doesn't account for someone refreshing the page 30,000 times.

What is the best way to go about this? Does php have any sort of way to see if a visit is unique?

I am using php/codeigniter/mysql/html/css/javascript/jquery.

+2  A: 

You could store if the client has already visited the page in a session variable, and run the same views query. Now this won't prevent a user from closing the browser, reopening, and returning, but it should provide a decent level of protection. If that's not enough you can move to cookies, followed by user registration, or IP tracking.

Okay, the session approach. On your page you run:

session_start();
if(!isset($_SESSION['page_views']['some_unique_string'])){
  $_SESSION['page_views']['some_unique_string'] = true;
  // update your database
}

Note that you'll want to make sure 'some_unique_string' is unique to the page your tracking views on.

The approach is nearly identical for cookies.

Mark E
this would probably be a good start, i would prefer to avoid using cookies if not necessary.
Roeland
The session variable doesn't require cookies.
Mark E
A: 

If you want to make sure the page views are unique you have to store the remote IPs of all the page views. There's really no other reliable way to know if a visitor has visited or not. Placing a cookie on their system will fail at a later date for a million different reasons, they cleared their cookies, they used a different browser, etc. You need a views table with a foreign key back to whatever you're counting views for.

You can find all of this sort of code in my free rated images script here:

http://destiney.com/php

Here's a demo:

http://ratedsite.com/

Look at the addImageRating.php script.

gdonald
wouldnt this be fairly resource intensive..
Roeland
This is not a perfect approach because some ISPs don't give each user a dedicated IP. This can be common at universities and used to be a big issue with AOL. That being said, I also prefer counting unique IPs to determine uniqueness in my site. Just understand there are caveats.
Gattster
Regarding your comment about it being resource intensive: If you need a solution that's not resource intensive, then use cookies. If you want to make it harder for somebody to exploit your voting system, require a fresh IP for each vote. You have to decide on a case-by-case basis.
Gattster
makes sense. since i will only be using this for an item "popularity" count, i think the session method will work, especially since all the products are from that particular company. (don't have to worry about a vendor trying to bloat their popularity)
Roeland