现在的位置: 首页 > 综合 > 正文

cookie彻底研究

2012年01月31日 ⁄ 综合 ⁄ 共 5340字 ⁄ 字号 评论关闭

网摘:http://hi.baidu.com/sshsoft/blog/item/0a02633eaca8d43b70cf6cd9.html

 1.标准方法

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4.     <title>标准的cookie操作</title>
  5.     <script type="text/javascript">
  6.         //设置cookie名,值,过期时间
  7.         function setCookie(c_name,value,expiredays)
  8.         {
  9.             var exdate=new Date()
  10.             exdate.setDate(exdate.getDate()+expiredays)
  11.             document.cookie=c_name+ "=" +escape(value)+
  12.             ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  13.         }
  14.         //取得cookie值
  15.         function getCookie(c_name)
  16.         {
  17.             if (document.cookie.length>0)
  18.               {
  19.               c_start=document.cookie.indexOf(c_name + "=")
  20.               if (c_start!=-1)
  21.                 { 
  22.                 c_startc_start=c_start + c_name.length+1 
  23.                 c_end=document.cookie.indexOf(";",c_start)
  24.                 if (c_end==-1) c_end=document.cookie.length
  25.                 return unescape(document.cookie.substring(c_start,c_end))
  26.                 } 
  27.               }
  28.             return ""
  29.         }
  30.         //使用上面两个函数,设置过期时间为一年。
  31.         function checkCookie()
  32.         {
  33.             username=getCookie('username')
  34.             if (username!=null && username!="")
  35.               {alert('Welcome again '+username+'!')}
  36.             else 
  37.               {
  38.                   username=prompt('Please enter your name:',"")
  39.                   if (username!=null && username!="")
  40.                     {
  41.                         setCookie('username',username,365)
  42.                     }
  43.               }
  44.         }
  45.     </script>
  46. </head>
  47. <body>
  48. <input type="button" value="cookie" onclick="checkCookie();" />
  49. </body>
  50. </html>

2.猴子的方法

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4.     <title>js操作cookie</title>
  5.     <script type="text/javascript">
  6.     //猴子的方法
  7.     /*取出Cookie*/
  8.     function getCookie(name){var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));if(arr!=null)return unescape(arr[2]);return ""}
  9.     /*设置Cookie*/
  10.     function setCookie(name,value){var exp =new Date();exp.setTime(exp.getTime()+30*24*60*60*1000);document.cookie=name+"="+escape(value)+";expires="+ exp.toGMTString()}
  11.     </script>
  12. </head>
  13. <body>
  14. <input type="button" onclick='setCookie("k1","khello")' value="set" />
  15. <input type="button" onclick='alert(getCookie("k1"))' value="get" />
  16. </body>
  17. </html>

3.C#和js相互操作cookie示例

前台

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="js_cs_cookie.aspx.cs" Inherits="js_js_cs_cookie" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>c#和js互操作cookie</title>
  6.     <script type="text/javascript">
  7.         //猴子的方法
  8.         /*取出Cookie*/
  9.         function getCookie(name){var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));if(arr!=null)return unescape(arr[2]);return ""}
  10.         /*设置Cookie*/
  11.         function setCookie(name,value){var exp =new Date();exp.setTime(exp.getTime()+30*24*60*60*1000);document.cookie=name+"="+escape(value)+";expires="+ exp.toGMTString()}
  12.     </script>
  13. </head>
  14. <body>
  15.     <form id="form1" runat="server">
  16.     <div>   
  17.     <input type="button" onclick='setCookie("k1","khello")' value="set" />
  18.     <input type="button" onclick='alert(getCookie("k2"))' value="get" />
  19.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  20.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
  21.     </form>
  22. </body>
  23. </html>

后台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class js_js_cs_cookie : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.     }
  16.     protected void Button1_Click(object sender, EventArgs e)
  17.     {
  18.         if (this.TextBox1.Text.Trim() == "get")
  19.             Response.Write(getCookie("k1"));
  20.         else
  21.             setCookie("k2""khello1");
  22.     }
  23.     /// <summary>
  24.     /// 增加一下Cookie
  25.     /// </summary>
  26.     /// <param name="strName"></param>
  27.     /// <param name="strValue"></param>
  28.     /// <returns></returns>
  29.     public bool setCookie(string strName, string strValue)
  30.     {
  31.         try
  32.         {
  33.             HttpCookie Cookie = new HttpCookie(strName);
  34.             Cookie.Expires = DateTime.Now.AddDays(100);//过期时间设置
  35.             Cookie.Value = System.Web.HttpContext.Current.Server.UrlEncode(strValue);//进入Url编码。
  36.             System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
  37.             return true;
  38.         }
  39.         catch
  40.         {
  41.             return false;
  42.         }
  43.     }
  44.     /// <summary>
  45.     /// 取得Cookie值,经过Url解码。
  46.     /// </summary>
  47.     /// <param name="strName"></param>
  48.     /// <returns></returns>
  49.     public string getCookie(string strName)
  50.     {
  51.         HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
  52.         if (Cookie != null)
  53.         {
  54.             return System.Web.HttpContext.Current.Server.UrlDecode(Cookie.Value);
  55.         }
  56.         else
  57.         {
  58.             return "无";
  59.         }
  60.     }
  61. }

抱歉!评论已关闭.