存入cookie代码:
HttpCookie cookie = new HttpCookie("myuser");
cookie.Values.Add("UserLevel", model.UserLevel.ToString());
cookie.Values.Add("userLoginPhone", model.RegisterPhone);
cookie.Values.Add("UserId", model.UserId.ToString());
cookie.Values.Add("UserNickName", model.NickName);
cookie.Expires = DateTime.Now.AddDays(7);
Response.AppendCookie(cookie);
调用代码:
getCookie("UserId", "&")
& 为截取分隔符
js获取cookie代码:
function getCookie(cookie_name, fhao) {
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name); //索引的长度
if (cookie_pos != -1) {
cookie_pos += cookie_name.length + 1;
var cookie_end = allcookies.indexOf(fhao, cookie_pos);
if (cookie_end == -1) {
cookie_end = allcookies.length;
}
var value = unescape(allcookies.substring(cookie_pos, cookie_end));
}
return value;
}