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.
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:
Note that you’ll want to make sure
'some_unique_string'is unique to the page you’re tracking views on.The approach is nearly identical for cookies.