cookie实现自动登陆,切换账号功能

博客内容提及切换账号指向6.html页面,可能与账号切换的操作流程或相关页面跳转有关。
//登陆首页
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		账号:<input type="text" id="userId"/><br />
		密码: <input type="text" id="userPwd"/><br/>
		<input type="button" value="登陆" id="btn" />
		<input type="checkbox" id="ched" />自动登陆
	</body>
</html>
<script>
	
	window.onload=function(){
		let strcookie=document.cookie;
		let arrcookie=strcookie.split("; ");
		for(let i=0;i<arrcookie.length;i++){
			let arrcook=arrcookie[i].split("=");
			if(arrcook[0]==""){
				location.href = "6.html";
			}else{
				location.href ="5.html";
			}
		}
	}
	let oId=document.getElementById("userId");
	let oPwd=document.getElementById("userPwd");
	let oBtn=document.getElementById("btn");
	let oChed=document.getElementById("ched");
	oBtn.onclick=function(){
		//判断是否选中自动登陆
		if(oChed.checked){
			//获取当前时间
			let date=new Date();
		    //改变Date对象的日期setDate, 返回日期getDate() 
			date.setDate(date.getDate()+7);
			//设置有效时间name=xxx;expires=xxx,expires:有效期
			document.cookie="Id="+oId.value+";expires="+date;
			document.cookie="Pwd="+oPwd.value+";expires="+date;
			location.href = "5.html";
		}else{
				document.cookie="Id="+oId.value;
				document.cookie="Pwd="+oPwd.value;
				location.href = "5.html";
		
		}
	}
</script>



//登陆成功之后的跳转页面---》5.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<p ><span id="spa"></span>你好</p>
	<input type="button" value="切换其他账号登陆" id="btn"/> 
	</body>
</html>
<script>
	let ospa=document.getElementById("spa");
	let cookieName=null;
	let cookiePwd=null;
	//获取当前浏览器中存在的cookie
	let strcookie=document.cookie;
	let arrcookie=strcookie.split("; ");
	for(let i=0;i<arrcookie.length;i++){
		let arrcook=arrcookie[i].split("=");
		if(arrcook[0]=="Id"){
			//将已存在的cookie的id值付给body里面的span
			ospa.innerHTML=arrcook[1];
		}
	}
	//删除cookie,将cookie里面的键值对设置为"";
	let obtn=document.getElementById("btn");
	obtn.onclick=function(){
	document.cookie="Id=''";
	document.cookie="Pwd=''";
	location.href = "6.html";
	}
</script>

//切换账号 —>6.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		账号:<input type="text" id="userId"/><br />
		密码: <input type="text" id="userPwd"/><br/>
		<input type="button" value="登陆" id="btn" />
		<input type="checkbox" id="ched" />自动登陆
	</body>
</html>
<script>
	let oId=document.getElementById("userId");
	let oPwd=document.getElementById("userPwd");
	let oBtn=document.getElementById("btn");
	let oChed=document.getElementById("ched");
	oBtn.onclick=function(){
		//判断是否选中自动登陆
		if(oChed.checked){
			//获取当前时间
			let date=new Date();
		    //改变Date对象的日期setDate, 返回日期getDate() 
			date.setDate(date.getDate()+7);
			//设置有效时间name=xxx;expires=xxx
			document.cookie="Id="+oId.value+";expires="+date;
			document.cookie="Pwd="+oPwd.value+";expires="+date;
			location.href = "5.html";
		}else{
				document.cookie="Id="+oId.value;
				document.cookie="Pwd="+oPwd.value;
				location.href = "5.html";
		
		}
	}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值