欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# javascript 读写Cookie的方法

程序员文章站 2024-02-13 09:55:16
第一:写cookies 复制代码 代码如下:response.cookies["username"].value="guest"; response.cookies["us...

第一:写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(";")));  
     }     
   }