html+js保存账号密码到Cookie

参考:https://blog.csdn.net/JHON_03/article/details/76563212

为了用户体验度,增加了账号密码记录功能,本文为了使我加深印象使用,参考里写的更详细.

在这里插入图片描述
流程点击记住密码,下次登录输入用户名自动添加密码.

<input type="text" name="username" id="LAY-user-login-username" lay-verify="required" onblur="showpassword()" placeholder="用户名" class="layui-input">
<input type="password" name="password" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
<input type="checkbox" name="remember" id="remember" lay-skin="primary" title="记住密码">


登录成功后触发

在这里插入图片描述

 //触发
 function remember() {
	  if(document.getElementById("remember").checked){
		  SetCookie($("#LAY-user-login-username").val(),$("#LAY-user-login-password").val());
	  }
  }
  //保存
  function SetCookie (name, value) {
	  var exp = new Date();
	  exp.setTime(exp.getTime() + (30*24*60*60*1000));
	  window.document.cookie = name + "=" + escape (value) + "; expires=" + exp.toGMTString()+";path=/";
  }
  //用户名输入完成自动读取cookie里密码
  function showpassword() {
	 var p=GetCookie($("#LAY-user-login-username").val());
	  if(p!=null){
		  $("#LAY-user-login-password").val(p);
	  }
  }
  function GetCookie (name) {
	  var arg = name + "=";
	  var alen = arg.length;
	  var clen = window.document.cookie.length;
	  var i = 0;
	  while (i < clen)
	  {
		  var j = i + alen;
		  if (window.document.cookie.substring(i, j) == arg) return getCookieVal (j);
		  i = window.document.cookie.indexOf(" ", i) + 1;
		  if (i == 0)
			  break;
	  }
	  return null;
  }
  function getCookieVal (offset) {
	  var endstr = window.document.cookie.indexOf (";", offset);
	  if (endstr == -1)
		  endstr = window.document.cookie.length;
	  return unescape(window.document.cookie.substring(offset, endstr));
  }
  </script>
您好,要实现自动获取上次登录的账号密码并回显到页面上,可以通过以下步骤实现: 1.将用户的账号密码保存Cookie中,在用户下一次访问网站时,从Cookie中读取账号密码信息。 2.在后端代码中读取Cookie中的账号密码信息,将其传递到前端。 3.在前端代码中使用JavaScript读取后端传递的账号密码信息,并将其填充到相应的文本框中。 下面是一个简单的示例: 后端代码: ```java // 从Cookie中读取账号密码信息 Cookie[] cookies = request.getCookies(); String username = null; String password = null; if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("username")) { username = cookie.getValue(); } else if (cookie.getName().equals("password")) { password = cookie.getValue(); } } } // 将账号密码信息传递到前端 request.setAttribute("username", username); request.setAttribute("password", password); ``` 前端代码: ```html <!-- 在文本框中回显账号密码信息 --> <input type="text" id="username" value="${username}"> <input type="password" id="password" value="${password}"> <script> // 读取后端传递的账号密码信息 var username = document.getElementById("username").value; var password = document.getElementById("password").value; // 将账号密码填充到文本框中 document.getElementById("username").value = username; document.getElementById("password").value = password; </script> ``` 注意,为了保护用户的账号密码安全,应该对密码进行加密后再保存Cookie中。另外,为了避免用户的账号密码被其他人恶意获取,应该设置Cookie的有效期,并在用户退出登录时删除Cookie
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值