js实现七天免登陆

这篇博客通过JavaScript展示了如何实现七天免登录的逻辑。当用户访问页面时,如果URL中携带了用户名和密码参数,或者用户的cookie中存在用户信息,页面将显示“登陆成功,欢迎XXX到此一游”的消息,从而实现免登录的效果。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body{background: #CCCCCC;}
.box{margin: 20px auto; width: 300px;font-size: 14px;}
.box input{margin-bottom: 20px; outline: none;}
</style>
</head>
<body>
<div class="box">
<input type="text" id="name" value="" /><br />
<input type="password" name="password" id="password" value="" /><br />
七天免登陆?<input type="checkbox" id="save" value="" />
<input type="button" id="login" value="login" />
</div>



<script type="text/javascript">
var _login = document.getElementById("login");
var _name = document.getElementById("name");
var _password = document.getElementById("password");


_password.onblur = function(){
if(/\w{6,20}/g.test(this.value)){
_login.onclick = function(){
if(_name.value.length>0 && _password.value.length>=6 ){
document.cookie = "user="+_name.value+";expires="+new Date(new Date().getTime()+7*24*3600*1000);
document.cookie = "password="+_password.value+";expires="+new Date(new Date().getTime()+7*24*3600*1000);

window.location = "cont.html?name="+_name.value+"&password="+_password.value;
}else{
alert('请重新输入用户名和密码');
}
}
}else{
alert("密码错误,请重新输入")
}


}
</script>

</body>

</html>



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
if(window.location.search.length>0){
var _body = document.getElementsByTagName('body')[0];
var _arr = window.location.search.replace("?","").split("&");
var _name = _arr[0].split("=")[1];
var _pwd = _arr[1].split("=")[1];
_body.innerHTML = "登陆成功,欢迎"+_name+"到此一游";
}else{
var _cook = document.cookie;
if(_cook.indexOf('user')>=0){
var _arr1 = _cook.split(';');
var _name = '';
for(var i=0;i<_arr1.length;i++){
if(_arr1[i].indexOf('user')>=0){
_name = _arr1[i].split('=')[1];
}
}
document.getElementsByTagName('body')[0].innerHTML = "登陆成功,欢迎"+_name+"到此一游"
}
}
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值