说明
===================================================================
要学会用cookies,因为Cookies是放客户端的,随用户访问回传,网站性能基本无影响.
中文名称为小型文本文件或小甜饼,指某些网站为了辨别用户身份而储存在用户本地终端(Client Side)上的数据(通常经过加密)。定义于RFC2109。为网景公司的前雇员Lou Montulli在1993年3月所发明
Cookies一词用在程序设计中是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术。当你浏览某网站时,由Web服务器置于你硬盘上的一个非常小的文本文件,它可以记录你的用户ID、密码、浏览过的网页、停留的时间等信息。当你再次来到该网站时,网站通过读取Cookies,得知你的相关信息,就可以做出相应的动作,如在页面显示欢迎你的标语,或者让你不用输入ID、密码就直接登录等等。从本质上讲,它可以看作是你的身份证。保存的信息片断以"名/值"对(name-value pairs)的形式储存,一个"名/值"对仅仅是一条命名的数据。
实际项目用例
===================================================================
后台代码 将需要用到的数据赋值给 名为countTotal 和 countPage 的Cookies
Response.Cookies["countTotal"].Value = urList.Count().ToString();
Response.Cookies["countTotal"].Expires = DateTime.MinValue;
Response.Cookies["countPage"].Value = Convert.ToString((urList.Count() / pageSize) + 1);
Response.Cookies["countPage"].Expires = DateTime.MinValue;
前台界面取值 重点就一句 通过 getCookie()方法将Cookie名称传进去获取其值,然后用setInterval()方法定时刷新数据.
function getCookie(cookie_name) {
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name); //索引的长度
// 如果找到了索引,就代表cookie存在,
// 反之,就说明不存在。
if (cookie_pos != -1) {
// 把cookie_pos放在值的开始,只要给值加1即可。
cookie_pos += cookie_name.length + 1; //这里我自己试过,容易出问题,所以请大家参考的时候自己好好研究一下。。。
var cookie_end = allcookies.indexOf(";", cookie_pos);
if (cookie_end == -1) {
cookie_end = allcookies.length;
}
var value = unescape(allcookies.substring(cookie_pos, cookie_end)); //这里就可以得到你想要的cookie的值了。。。
}
return value;
}
setInterval(function () { $("#labText").text(getCookie("countTotal")); }, 1000);
===================================================================
参数说明
===================================================================
设置到期时间
//20分钟后到期
TimeSpan ts=new TimeSpan(0,0,20,0)
Response.Cookies["MyCookie"].Expires=DateTime.Now.Add(ts);
//一个月后
Response.Cookies["MyCookie"].Expires=DateTime.Now.AddMonths(1);
//指定日期
Response.Cookies["MyCookie"].Expires=DateTime.Parse("10/26/2012");
//永不过期
Response.Cookies["MyCookie"].Expires=DateTime.MaxValue;
//关闭浏览器后过期
Response.Cookies["MyCookie"].Expires=DateTime.MinValue;
===================================================================
简单用例
===================================================================
后台:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnWrite_Click(object sender, EventArgs e)
{
string UserIP = Request.UserHostAddress.ToString();
Response.Cookies["IP"].Value = UserIP;
}
protected void btnRead_Click(object sender, EventArgs e)
{
this.Label1.Text = Request.Cookies["IP"].Value;
}
}
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cookie</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnWrite" runat="server" OnClick="btnWrite_Click" Text="将用户IP写入Cookie"
Width="146px" /><br />
<asp:Button ID="btnRead" runat="server" OnClick="btnRead_Click" Text="将用户IP从Cookie中读出"
Width="147px" /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div>
</form>
</body>
</html>
===================================================================
加密用例
===================================================================
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="写入Cookie"
οnclick="Button1_Click" />
</div>
<div>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="读出Cookie" οnclick="Button2_Click" />
</div>
<div>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="加密Cookie" οnclick="Button3_Click" />
</div>
<asp:Button ID="Button4" runat="server" Text="Cookie对象属性使用"
οnclick="Button4_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;//md5
using System.Web.Security;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
HttpCookie makecookie = new HttpCookie("Cookie");
makecookie.Value = this.TextBox1.Text;
Response.Cookies.Add(makecookie);
Response.Write("<script>alert('写入成功!')</script>");
}
else
{
Response.Write("<script>alert('请先写入Cookie!')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
HttpCookie readcookie = Request.Cookies["Cookie"];
TextBox2.Text = readcookie.Value;
}
else
{
Response.Write("<script>alert('请先写入Cookie!')</script>");
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string strPwd = TextBox1.Text;
Response.Cookies["strPwd"].Value = FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd, "md5");
TextBox3.Text = "加密值为:" + Response.Cookies["strPwd"].Value.ToString();
}
else
{
Response.Write("<script>alert('请先写入Cookie!')</script>");
}
}
protected void Button4_Click(object sender, EventArgs e)
{
HttpCookie makecookie = new HttpCookie("Cookie");
makecookie.Value = this.TextBox1.Text;
Label1.Text = "指定Cookie的有效时间:" + makecookie.Expires.ToString() + "<br>"
+"指定Cookie的名称:"+makecookie.Name+"<br>"
+"指定Cookie的名称:"+makecookie.Value+"<br>"
+ "指定Cookie的名称:" + makecookie.Path;
Response.Cookies.Add(makecookie);
}
}
===================================================================