“js操作cookie” 与 “php操作cookie”,两种实现方法
一、js操作cookie:
function SetCookie(name,value,days)//两个参数,一个是cookie的名子,一个是值
{
var Days = 30;
if(typeof(days)=="undefined"||isNaN(days))
Days=parseInt(days.toString());
//此 cookie 将被保存 30 天 -1为浏览器关闭
if(Days!=-1){
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}else{
document.cookie = name + "="+ escape (value) + ";expires=-1";
}
}
{
var Days = 30;
if(typeof(days)=="undefined"||isNaN(days))
Days=parseInt(days.toString());
//此 cookie 将被保存 30 天 -1为浏览器关闭
if(Days!=-1){
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}else{
document.cookie = name + "="+ escape (value) + ";expires=-1";
}
}
function getCookie(name)//取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;}
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;}
function delCookie(name)//删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - (86400 * 1000 * 1));
var cval=getCookie(name);
if(cval!=null)
document.cookie = name + "="+ escape (cval) + ";expires="+exp.toGMTString();}
{
var exp = new Date();
exp.setTime(exp.getTime() - (86400 * 1000 * 1));
var cval=getCookie(name);
if(cval!=null)
document.cookie = name + "="+ escape (cval) + ";expires="+exp.toGMTString();}
调用实例: 登陆时“记住我”设置cookie
//头部登陆框的验证函数
function checkform(objF){
var cookieName_username="LOGIN_USER_NAME";
var cookieName_password="LOGIN_PASSWORD";
delCookie(cookieName_username);//删除用户名Cookie
delCookie(cookieName_password);//删除密码
if(objF.autologin.checked)
{
SetCookie(cookieName_username,objF.uname.value.trim(),7);//保存到Cookie中 保存7天
SetCookie(cookieName_password,objF.password.value.trim(),7);
}
}
function checkform(objF){
var cookieName_username="LOGIN_USER_NAME";
var cookieName_password="LOGIN_PASSWORD";
delCookie(cookieName_username);//删除用户名Cookie
delCookie(cookieName_password);//删除密码
if(objF.autologin.checked)
{
SetCookie(cookieName_username,objF.uname.value.trim(),7);//保存到Cookie中 保存7天
SetCookie(cookieName_password,objF.password.value.trim(),7);
}
}
调用实例: 获取cookie自动填充登陆框
function getUserInfoByCookie(){
var uname=getCookie(cookieName_username);
var upass=getCookie(cookieName_password);
if(uname!=null&&!uname.toString().isnullorempty())
if(upass!=null&&!upass.toString().isnullorempty()){
document.getElementById("uname").value=uname;
document.getElementById("password").value=upass; }}
var uname=getCookie(cookieName_username);
var upass=getCookie(cookieName_password);
if(uname!=null&&!uname.toString().isnullorempty())
if(upass!=null&&!upass.toString().isnullorempty()){
document.getElementById("uname").value=uname;
document.getElementById("password").value=upass; }}
二、php操作cookie
实例:登陆时设置cookie
if($autologin=='1')
{
setcookie("account", $account, time()+1000,"/path/");
setcookie("password", $password, time()+1000,"/path/");}
{
setcookie("account", $account, time()+1000,"/path/");
setcookie("password", $password, time()+1000,"/path/");}
实例:退出时删除cookie
setcookie("account", "", time()-3600,"/path/");
setcookie("password", "", time()-3600,"/path/");
setcookie("password", "", time()-3600,"/path/");
实例:index.php获取cookie
if (isset($_COOKIE["account"])&&isset($_COOKIE["password"])) {
$account=$_COOKIE["account"];
$account=$_COOKIE["password"];
}
$account=$_COOKIE["password"];
}