Login页面设置Cookie:
if(json.result=="true") { if($("input[type=‘checkbox‘]").is(‘:checked‘)) {setCookie(‘sapid‘,json.zstaff_id,30)} else {setCookie(‘sapid‘,json.zstaff_id,0.05)/*1小时后过期*/} alert("登陆成功"); /*登陆成功后定向到主页*/ window.location.href = "index.html"; } else {alert("账号或密码错误,登陆失败!");} //设置Cookie function setCookie(cname, cvalue,cdate) { var exdays=cdate; var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires="+d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires; }
其它页面获取Cookie判断:
<script type="text/javascript"> $(document).ready(function(){ /*未加载成功时不显示*/ var userid = getCookie("sapid"); if (userid == "") { $(".alertDiv_hint").html("<p>您无权浏览该页面,请登陆后再试!</p>"); $(".alertDiv").show(); setTimeout("location.href=‘login.html‘",1000); } }); //获取cookie function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(‘;‘); for(var i=0; i<ca.length; i++) { var c = ca[i]; while (c.charAt(0)==‘ ‘) c = c.substring(1); if (c.indexOf(name) != -1) return c.substring(name.length, c.length); } return ""; } //清除cookie function delCookie(name){ var date = new Date(); date.setTime(date.getTime() - 10000); document.cookie = name + "=a; expires=" + date.toGMTString(); } </script>
时间: 2024-10-29 19:06:34