I want to know how sites like stackoverflow.com or imgur.com manage the page views of their article. i.e. once i view it and then no matter how many times i refresh it.. the page view count won't increase. I would also like to implement it for my image site and would also like to know when should i update views in database.



This is normally handled using session variables. When the user enters the site for the first time, a session is created.

Every time you view view an article or a page, the site checks whether the id of the page is stored in the session variable. If it isn't, then the view is counted and the id stored in the session variable. Otherwise, the view count is skipped.

The way you implement this feature depends on the programming language you used to create your site (Ruby, Python, Java, PHP...).

  • Do sites really store every page you visit in a session variable? That's a lot of overhead to send to the server on every page request, if a user has visited lots of pages. – DisgruntledGoat Nov 3 '11 at 20:33
  • You don't need to store every page, but just the ID corresponding to the question record. Also, session variables usually are stored on the server, you just pass the session_id between the client and the server. – Simone Carletti Nov 3 '11 at 23:09
  • Ah OK, I was getting mixed up with cookies. And thinking about it, storing 100+ question IDs should be less than 1KB. – DisgruntledGoat Nov 3 '11 at 23:19

Here's how I would do it:

  1. First check that it looks like a real person using a browser and not a search engine bot (e.g. check the user agent string for "bot").
  2. Store the IP address in a database with each visited page, for up to 24 hours.
  3. On each page load, if this IP address has not viewed this page in the last 24 hours then add 1 to the view count.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.