Chaddeus
Chaddeus

Reputation: 13366

How best to block multiple "counts" on a counter?

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.

Upvotes: 0

Views: 911

Answers (3)

John Farrell
John Farrell

Reputation: 24754

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.

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196187

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

Upvotes: 2

driis
driis

Reputation: 164331

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;
}

Upvotes: 2

Related Questions