日期:2014-05-17  浏览次数:20477 次

避免频繁刷新来增加访问量
设计一个网站访问总量计数器,并避免用户通过频繁刷新、访问来增加访问量。(提示:使用Cookie对象确认用户行为)
题目是这样说的,首先老师给的提示我就不是很清楚,不知道如何确认用户行为。
==============================================================================
C# code
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

            string IP = Request.UserHostAddress;
            Response.Cookies["ipname"].Value = IP;
            Response.Cookies["ipname"].Expires = DateTime.Now.AddSeconds(4);
            Application.Lock();
            Application["counter"] = 1;
            if (Request.Cookies["ipname"].Value.ToString() != IP)
            {
                Application["counter"] = Convert.ToInt32(Application["counter"]) + 1;
            }
            else Application["counter"] = Convert.ToInt32(Application["counter"]) + 0;
            Label1.Text = "您是第" + Application["counter"] + "位访问者!";
            
            Application.UnLock();
        
    }
}


我是这么写的,限制IP好像太死了,不太实际,而且本以为Response.Cookies["ipname"].Expires = DateTime.Now.AddSeconds(4);在4秒后删除cookies以便条件语句可以执行,但是想到每次加载页面时都会执行 Response.Cookies["ipname"].Value = IP;所以对cookie有效期的限制就没用了。

=====================================================================================================
希望哪位好心的人给点详细的解答,初学这个,谢谢了。

------解决方案--------------------
通过ip来限制,可以累计访问同一个页面的时间间隔,一段时间内同个IP的就不计数
------解决方案--------------------
C# code
        //判断Cookie是否存在
        if (Request.Cookies["ipname"] == null)
        {
            Response.Cookies["ipname"].Value = "其实没意义";
            Response.Cookies["ipname"].Expires = DateTime.Now.AddSeconds(4);
            //初始化 Application["counter"]
            if (Application["counter"] == null)
            {
                Application["counter"] = 1;
                return;
            }
            Application.Lock();
            Application["counter"] = Convert.ToInt32(Application["counter"]) + 1;
            Application.UnLock();
        }
        lblAccessCount.Text = "您是第" + Application["counter"] + "位访问者!";

------解决方案--------------------
C# code
//判断Cookie是否存在
        if (Request.Cookies["ipname"] == null)
        {
            Response.Cookies["ipname"].Value = "其实没意义";
            Response.Cookies["ipname"].Expires = DateTime.Now.AddSeconds(4);
            //初始化 Application["counter"]
            if (Application["counter"] == null)
            {
                Application["counter"] = 1;
            }
            else
            {
                Application.Lock();
                Application["counter"] = Convert.ToInt32(Application["counter"]) + 1;
                Application.UnLock();
            }
        }
        lblAccessCount.Text = "您是第" + Application["counter"] + "位访问者!";

------解决方案--------------------
C# code
//判断Cookie是否存在
        if (Request.Cookies["ipname"] == null)
        {
            Response.Cookies["ipname"].Value = "其实没意义";
            Response.Cookies["ipname"].Expires = DateTime.Now.AddSeconds(4);
            //初始化 Application["counter"]
            if (Application["counter"] == null)
            {
                Application["counter"] = 1;
            }
            else
            {