隻是記錄一下。。一會做項目要用。。。
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秒以内才能投票一次