views:

42

answers:

4

I've got a real cheesy counter, just adds one to the counter field when a visit hits it. It's not counting page hits, but it's still cheesy.

What I need to do is stop someone from just hitting refresh over and over. What's the simplest was to get this done? Cookies?

I'd prefer not to log every visitor's ip address, etc... something simple - in C# asp.net mvc.

+2  A: 

Yes, cookies would be a simple way to achieve that. Even simpler would be to simply set a value in Session - if that is set, this visit has been registered, so we should not increment the counter again.

This way you will also count a "visit" per session, which most often is the best measure for unique visits.

Pseudocode to implement:

if (Session["HasCountedThisVisitor"] == null) 
{
    counter++; 
    Session["HasCountedThisVisitor"] = true;
}
driis
ah, good call. They have to to kill their session completely between 'visits'. Maybe a combination of this and cookies. Thanks!
Chad
@Chad, remember that Session is implemented by cookies. So you will most likely want to use only either cookie or session. Use cookies, if you want to count unique _visitors_ (do not count subsequent visits by the same browser on same machine), use Session if you want to count unique _visits_ (as in, if the visitor comes back tomorrow she is counted again)
driis
Oh ya... sorry, was like 2am when I asked this question. Thanks for the answer!
Chad
+2  A: 

Cookie or a session variable that you check if it has been set, before increasing the counter..

Gaby
Thanks, seems to be the common recommendation.
Chad
A: 

If you don't wish to save any extra information into your server, then you need to use something to store data in the client side. I'm not saying its ideal, but yes, using cookies is potentially the simplest way to prevent "visit counter spam".

I suggest the following strategy for a quick-and-dirty counter implementation:

  • Use a cookie to mark if a visitor has been already logged ("firstVisit")
  • If the cookie has not been defined, add a visitor to your counter and create a cookie

Note that if someone has cookies disabled, you may still get duplicate visits. But hey, I didn't say this is trivial!

jsalonen
A: 

Why?

There are tons of free logging tools, some analyze your IIS logs, some give you some javascript to place on your pages. Google Analytics is pretty awesome and free.

You're reinventing the wheel here.

jfar
nah, I'm not logging hit/visits/pageviews... I do use Google Analytics for that. My counter counts an object within my DB. Thanks though.
Chad