首页 > php教程 > php手册 > C# javascript 读写Cookie

C# javascript 读写Cookie

WBOY
发布: 2016-06-06 20:01:49
原创
1154 人浏览过

第一:写Cookies Response.Cookies["UserName"].Value="Guest"; Response.Cookies["UserName"].Expires=DateTime.Now.AddDays(1); 第二:添加Cookies HttpCookie userName_zhengshu_Cookie = new HttpCookie("uname"); userName_zhengshu_Cookie.Value = "陈

  
第一:写Cookies

Response.Cookies["UserName"].Value="Guest";
Response.Cookies["UserName"].Expires=DateTime.Now.AddDays(1);

    
第二:添加Cookies

 HttpCookie userName_zhengshu_Cookie = new HttpCookie("uname");
        userName_zhengshu_Cookie.Value = "陈xx";


        HttpCookie userLession_zhengshur_Cookie = new HttpCookie("lcode");
        userLession_zhengshur_Cookie.Value = "chen jian ping";

        userName_zhengshu_Cookie.Path = "/";
        userLession_zhengshur_Cookie.Path = "/";

//限定有效期
        userLession_zhengshur_Cookie.Expires = DateTime.Now.AddDays(1);
        userName_zhengshu_Cookie.Expires = DateTime.Now.AddDays(1);

        Response.Cookies.Add(userLession_zhengshur_Cookie);
        Response.Cookies.Add(userName_zhengshu_Cookie);

//Cookies添加
 Response.AppendCookie(user_cookies);

//取值
Response.Write(Request.Cookies["uname"].Value);

 

//javascript写读Cookie
function Set_Cookie()
{
    var setValue="陈xx";
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+365);
    document.cookie="userLession"+ "=" +setValue+";expires="+exdate.toGMTString()+";path=/";
    alert("已经写入Cookie");
}

 function Get(){     
     var cookieString=new String(document.cookie)    
     var cookieHeader = "userLession="    
     var beginPosition = cookieString.indexOf(cookieHeader)    
     if(beginPosition != -1)
     {    
       alert(cookieString.substring(cookieHeader.length,cookieString.indexOf(";"))); 
     }    
     
   }    

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板