How best to block multiple “counts” on a counter?

爷,独闯天下 提交于 2019-12-08 09:16:12

问题


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.


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3954275/how-best-to-block-multiple-counts-on-a-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!