天天看点

ASP.NET简单投票代码

只是记录一下。。一会做项目要用。。。

if (Request.Cookies["tp"] != null && Request.Cookies["tp"].Value == "true")  

        {  

            Alert("您已经投票过了!");  

            return;  

        }  

        HttpCookie cookie = new HttpCookie("tp");  

        cookie.Value = "true";  

        cookie.Expires = DateTime.Now.AddSeconds(10);  

        Response.Cookies.Add(cookie);  

        Alert("投票成功!");  

原理就是:

1、判断cookie中的某个指是否存在,该值表示是否投票过,存在则弹出提示已经投票过,退出执行

2、不存在,可以投票,建立cookie,上面的代码是设置cookie的过期时间是10秒后

    这样就只能是10秒以内才能投票一次

继续阅读