Web浏览器和服务器使用HTTP协议进行通信。HTTP是无状态协议,即,它不会在客户端发出的多个请求中维护客户端的数据。客户端和服务器之间的完整请求-响应周期定义为会话。Cookies是浏览器用于存储与用户会话有关的数据的默认机制。
怎么运行的?
服务器以cookie的形式向访问者的浏览器发送一些数据。浏览器可以接受该cookie。如果是这样,它将以纯文本记录的形式存储在访问者的硬盘上。现在,当访问者到达您网站上的另一个页面时,浏览器会将相同的cookie发送到服务器以进行检索。一旦检索到,您的服务器就会知道/记住以前存储的内容。
Cookies是5个可变长度字段的纯文本数据记录。
-
Expires -Cookie过期的日期。如果为空,则cookie将在访问者退出浏览器时过期。
-
Domain -您站点的域名。
-
Path -设置cookie的目录或网页的路径。如果要从任何目录或页面检索cookie,则该字段可以为空。
-
Secure-如果此字段包含“secure”一词,则只能使用安全服务器检索cookie。如果此字段为空白,则不存在此类限制。
-
Name = Value -Cookies以键值对的形式设置和检索。
Cookies最初是为CGI编程设计的。Cookie中包含的数据会在Web浏览器和Web服务器之间自动传输,因此服务器上的CGI脚本可以读取和写入存储在客户端的Cookie值。
JavaScript还可以使用Document对象的cookie属性来操作cookie。JavaScript可以读取,创建,修改和删除适用于当前网页的cookie。
储存Cookies
创建cookie的最简单方法是将一个字符串值分配给document.cookie对象,如下所示。
"document.cookie = "key1 = value1; key2 = value2; expires = date";
在这里,“ expires”属性是可选的。如果为该属性提供有效的日期或时间,则cookie将在给定的日期或时间过期,此后,将无法访问cookie的值。
注意 -Cookie值不能包含分号,逗号或空格。出于这个原因,可能想在将值存储在cookie中之前使用JavaScriptscape ()函数对值进行编码。如果这样做,则在读取Cookie值时还必须使用相应的unescape()函数。
<html> <head> <script type = "text/javascript"> function WriteCookie() { if( document.myform.customer.value == "" ){ alert ("Enter some value!"); return; } cookievalue = escape(document.myform.customer.value) + ";"; document.cookie = "name = " + cookievalue; document.write ("Setting Cookies : " + "name = " + cookievalue ); } </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set" onclick = "WriteCookie();"/> </form> </body> </html>
读取Cookies
读取cookie就像编写cookie一样简单,因为document.cookie对象的值就是cookie。因此,想访问cookie,就可以使用此字符串。该document.cookie中的字符串将保持用分号,其中名称是一个cookie的名称和值是它的字符串值分隔的名称=值对的列表。可以使用字符串的split()函数将字符串分成键和值,如以下示例所示。
<html> <head> <script type = "text/javascript"> function ReadCookie() { var allcookies = document.cookie; document.write ("All Cookies : " + allcookies ); } // Get all the cookies pairs in an array cookiearray = allcookies.split(';'); // Now take key value pair out of this array for(var i = 0; i<cookiearray.length; i++) { name = cookiearray[i].split('=')[0]; value = cookiearray[i].split('=')[1]; document.write ("Key is : " + name + " and Value is : " + value); } </script> </head> <body> <form name = "myform" action = ""> <p> click the following button and see the result:</p> <input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/> </form> </body> </html>
设置Cookie有效期
可以通过设置有效期限并将有效期限保存在cookie中,从而将cookie的寿命延长到当前浏览器会话之外。这可以通过将“ expires”属性设置为日期和时间来完成。以下示例说明了如何将Cookie的有效期延长1个月。
<html> <head> <script type = "text/javascript"> function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() + 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name = " + cookievalue; document.cookie = "expires = " + now.toUTCString() + ";" document.write ("Setting Cookies : " + "name = " + cookievalue ); } </script> </head> <body> <form name = "formname" action = ""> Enter Cookie Name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form> </body> </html>
删除Cookie
有时您会想要删除一个cookie,以便后续尝试读取该cookie不会返回任何内容。只需要将到期日期设置为过去的某个时间。