views:

110

answers:

4

Hi, 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: http://stackoverflow.com/questions/1535261/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.

+2  A: 

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.

Arthur Frankel
Nice method, i never thought of using the session id. Although in my case this is not an option, the session id changes from time to time. But i will sure keep this in mind for further projects
Saif Bechan
Another problem with a session id is that I can open up firefox, opera, safari, ie and chrome and pad my numbers, or, write an automated program that will connect, go to the correct article, then start over, with a new session, since it doesn't actually persist a cookie, just pretends, with htmlunit.
James Black
+2  A: 

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.

James Black
Thank you for the spelling, and sorry about that. Nice answer, i think i can use this method on my site. Although i'm still having the question if it is better to write this to a log, or directly to the database. The article i read suggested it is better to write to the log, because there would be no database locks.
Saif Bechan
I would go with the database, as with a log then you have a problem with parsing. This should be a very fast lookup, as you can put it into a table with an index on the datetime. If you are concerned use a trigger, passing in the ip addr and page/article, and let the trigger make the decision.
James Black
+1  A: 

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.

Nick Johnson
A: 

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

Marius Burz