Question

i was wondering what the best way is to implement a hit counter for articles, products, etc. Now if someone visits the page the counter just adds one in the database. If someone refreshes the page it counts continuously, misleading results, and unnecessary reads, writes.

I was thinking of storing their ip, but i don't know how to model this in mysql. If i make a db record for each hit it will be enormous.

I have read this article: How to write an efficient hit counter for websites

The best answer was using a log and then update this log to db. But then again.

What is the best way to determine a new hit, is this with IP or another variable. And what is an acceptable amount of time to log the hit of particular user again.

Any other types of implementations are welcome too.

Was it helpful?

Solution

You could also just store the IP address and the time that you first logged it, and only increment if the time has been long enough, perhaps 30 minutes, then also increment the time related to the IP address.

OTHER TIPS

I suppose I would simply just use their session id to check for that. The session id is readily available and there's no need to do a database hit to retrieve it. An acceptable amount of time for me would be their next session. In their session I would keep track of the pages they hit. This way there's nothing in the database to check and the data goes away when the session goes away.

The simplest method: When the user first visits, increment the counter and send them a cookie. If you detect the cookie, don't increment the counter.

That's tricky. It's really impossible to get it 100% right:

  1. If you rely on cookies, what happens when they get deleted(1), expire(1) or when they are disabled(2)?
  2. If you rely on the IP, what happens with those people who reach your site through proxies? There are enough companies, ISPs that let the users out only through proxies (3) and/or (1)
  3. Session Id: when it's stored as a cookie see 1. ; when it's not, it gets reset each time the user comes to your site without having the Session Id GET/POST parameter set(1) (if the session is always bound to a user through an authentication process, that's a different case)

The most common implementation is by using cookies. Just remember it's not perfect.

(1) you'll get multiple hits from the same user
(2) you'll get lots of hits from the same user, unless you handle the clients that don't allow cookies differently
(3) you'll get only one hit from multiple users

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top