首页 > 代码库 > cookie记录

cookie记录

登录页面引用:

<script src="http://www.mamicode.com/encrypt.js"></script>
<script src="http://www.mamicode.com/jquery.cookie.js"></script>//若需要,请留言
登录页面jq:
var telphone = $(‘[name = "telphone"]‘).val();
var pwd = $(‘[name = "pwd"]‘).val();
//登录接口
$.post("/sign",{
login: telphone,
password: pwd
},function(data){
if(data.status == 200){
//登录成功后跳转到首页
if(parame == ‘index‘){//从首页点击登录跳转到登录页面的索引
//设置cookie
$.cookie(‘login‘,telphone,{ expires: 365, path: ‘/index.html‘ });
$.cookie(‘token‘,data.data.access_token,{ expires: 365, path: ‘/index.html‘ });
$.cookie(‘username‘,data.data.username,{ expires: 365, path: ‘/index.html‘ });
$.cookie(‘pwd‘,encrypt(pwd),{ expires: 365, path: ‘/index.html‘ }); //密码
window.location.href = http://www.mamicode.com/‘/index.html?login=‘+telphone+‘&access_token=‘+data.data.access_token+‘&username=‘+data.data.username+‘‘;
}

}
else{
alert(data.error);
}
})

首页页面同样引用:
<script src="http://www.mamicode.com/request.js"></script>
<script src="http://www.mamicode.com/encrypt.js"></script>
<script src="http://www.mamicode.com/jquery.cookie.js"></script>//若需要,请留言
首页jq:
var login = request(‘login‘);

var token = request(‘access_token‘);

var username = request(‘username‘);

var pwd;//加密密码
//判断cookie中是否有登录的记录
if($.cookie(‘login‘) == undefined || $.cookie(‘login‘) == ‘‘){

}else{
login = $.cookie(‘login‘);
}
if($.cookie(‘token‘) == undefined || $.cookie(‘token‘) == ‘‘){

}else{
token = $.cookie(‘token‘);
}
if($.cookie(‘username‘) == undefined || $.cookie(‘username‘) == ‘‘){

}else{
username = $.cookie(‘username‘);
}
if($.cookie(‘pwd‘) == undefined || $.cookie(‘pwd‘) == ‘‘){

}else{
pwd = $.cookie(‘pwd‘);
}

//首页点击退出登录
$(‘.denglu‘).click(function(){
$.cookie(‘login‘,‘‘,{ path: ‘/index.html‘ });
$.cookie(‘token‘,‘‘,{ path: ‘/index.html‘ });
$.cookie(‘username‘,‘‘,{ path: ‘/index.html‘ });
$.cookie(‘pwd‘,‘‘,{ path: ‘/index.html‘ });
window.location.href = http://www.mamicode.com/‘/index.html‘;
});


cookie记录