asp.net cookie實現頂踩功能代碼

來源:互聯網
上載者:User

asp教程.net cookie實現頂踩功能代碼

public void processrequest(httpcontext context)
        {
            bll.video_bll video_bll = new bll.video_bll();
            if (!string.isnullorempty(context.request.querystring["id"]))
            {
                int id = convert.toint32(context.request.querystring["id"]);
                string obj = context.request.querystring["fldname"];
                httpcookie cookie = context.request.cookies["info"];
                if (cookie != null && cookie.values["id"] == id.tostring())
                {
                    context.response.write("false");
                    context.response.end();
                }
                else
                {
                    httpcookie cookies = new httpcookie("info");
                    cookies.values["userip"] = tools.tools.getuseripaddress();
                    cookies.values["id"] = id.tostring();
                    cookies.expires = datetime.now.adddays(1);
                    context.response.cookies.add(cookies);
                    video_bll.updatedigbury(obj, id);
                    list<videoinfo> list = video_bll.getvideo(1, "and id=" + id);
                    if (list.count > 0)
                    {
                        switch (obj)
                        {
                            case "dig":
                                context.response.write(list[0].dig);
                                context.response.end();
                                break;
                            case "bury":
                                context.response.write(list[0].bury);
                                context.response.end();
                                break;
                            default:
                                context.response.write("無此資料");
                                break;
                        }
                    }
                }
            }
        }

//方法二

response.cookies["userinfo"]["username"] = "patrick";
response.cookies["userinfo"]["lastvisit"] = datetime.now.tostring();
response.cookies["userinfo"].expires = datetime.now.adddays(1);

httpcookie acookie = new httpcookie("userinfo");
acookie.values["username"] = "patrick";
acookie.values["lastvisit"] = datetime.now.tostring();
acookie.expires = datetime.now.adddays(1);
response.cookies.add(acookie);

//讀取 cookie
if(request.cookies["userinfo"] != null)
{
    label1.text =
        server.htmlencode(request.cookies["userinfo"]["username"]);

    label2.text =
        server.htmlencode(request.cookies["userinfo"]["lastvisit"]);
}

/*
cookie實現頂踩
每篇文章一天內一個使用者只能頂踩一次
用cookie實現了。
可是我點另外一篇文章的時候,可以頂踩,可是在回到原來那篇文章重新整理後,又可以頂踩了,還沒過一天。
原來是cookie當點擊另外一篇文章的時候重新執行個體化了

*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.