工作笔记:C#记录重复操作(如顶、踩等)

控制用户对某一条评论只能操作一次,如顶、踩、支持、反对等。


以下代码在实际使用中可能需要修改一下,核心是记录操作者的IP和操作的对象编号到COOKIE中,
在用户操作时,遍历COOKIE中的数据,看是否有对应的记录。

   public bool isRepeat(string commentId)
    {
        string userIp = Request.UserHostAddress;
        if (!isCookie())
        {
            createCookie(commentId, userIp);
            return false;
        }
        else if (!getCookInfo(commentId, userIp))
        {
            addCookie(commentId);
            return false;
        }
        else
        {
            return true;
        }
    }
    private bool isCookie()
    {
        if (Request.Cookies["cook_sustain_oppose"] == null)
            return false;
        return true;
    }
    private void createCookie(string commentId, string userIp)
    {
        HttpCookie cook = new HttpCookie("cook_sustain_oppose");
        cook.Values["oId" + commentId] = commentId;
        cook.Values["userIp"] = userIp;
        Response.Cookies.Add(cook);
    }
    private void addCookie(string commentId)
    {
        HttpCookie cook = Request.Cookies["cook_sustain_oppose"];
        cook.Values["oId" + commentId] = commentId;
        Response.Cookies.Add(cook);
    }
    private bool getCookInfo(string commentId, string userIp)
    {
        HttpCookie cook = Request.Cookies["cook_sustain_oppose"];
        if (cook.Values["oId" + commentId] == commentId && cook.Values["userIp"] == userIp)
        {
            return true;
        }
        return false;
    }

Tag标签: 源码COOKIE
posted on 2009-09-01 22:45 发布:水寒冰 阅读(490) 评论(0) 收藏 所属分类: ASP.NET(C#)
  • 评论
  • 点击刷新
  • [使用Ctrl+Enter键可以直接提交]

表情图标

[smile][confused][cool][cry][eek][angry][wink][sweat][lol][stun][razz][redface][rolleyes][sad][yes][no][heart][star][music][idea]
Advertise
Category
Time Counter

离十一还有

Recent Article
Statistics
Recent Comments
Archive
Links
Support
《良机》 鲜果阅读器订阅图标
 
TOP