JavaScript USES cookie to remember account passwords

  • 2020-06-01 08:19:31
  • OfStack

Many login functions have a "remember your password" function, which is nothing more than reading cookie.

The following shows the code of this function, the original author has not been able to study...

Test method: directly enter the account password, submit, refresh the page, and then enter the same account, it can be displayed


<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js COOKIE  Remember your account number or password </title>
<script type="text/javascript">
  window.onload=function onLoginLoaded() {
    if (isPostBack == "False") {
      GetLastUser();
    }
  }
   
  function GetLastUser() {
    var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID identifier 
    var usr = GetCookie(id);
    if (usr != null) {
      document.getElementById('txtUserName').value = usr;
    } else {
      document.getElementById('txtUserName').value = "001";
    }
    GetPwdAndChk();
  }
  // The client event is triggered when you click login 
   
  function SetPwdAndChk() {
    // Access account 
    var usr = document.getElementById('txtUserName').value;
    alert(usr);
    // Will be the last 1 User information written to Cookie
    SetLastUser(usr);
    // If the remember password option is selected 
    if (document.getElementById('chkRememberPwd').checked == true) {
      // Take the password value 
      var pwd = document.getElementById('txtPassword').value;
      alert(pwd);
      var expdate = new Date();
      expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
      // Writes the username and password to Cookie
      SetCookie(usr, pwd, expdate);
    } else {
      // If not, remember the password , It expires immediately 
      ResetCookie();
    }
  }
   
  function SetLastUser(usr) {
    var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
    var expdate = new Date();
    // The current time plus two weeks 
    expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
    SetCookie(id, usr, expdate);
  }
  // This method is called when the user name loses focus 
   
  function GetPwdAndChk() {
    var usr = document.getElementById('txtUserName').value;
    var pwd = GetCookie(usr);
    if (pwd != null) {
      document.getElementById('chkRememberPwd').checked = true;
      document.getElementById('txtPassword').value = pwd;
    } else {
      document.getElementById('chkRememberPwd').checked = false;
      document.getElementById('txtPassword').value = "";
    }
  }
  // take Cookie The value of the 
   
  function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
      var j = i + alen;
      //alert(j);
      if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
    }
    return null;
  }
  var isPostBack = "<%= IsPostBack %>";
   
  function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
  }
  // Written to the Cookie
   
  function SetCookie(name, value, expires) {
    var argv = SetCookie.arguments;
    // In this case, length = 3
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
  }
   
  function ResetCookie() {
    var usr = document.getElementById('txtUserName').value;
    var expdate = new Date();
    SetCookie(usr, null, expdate);
  }
</script>
</head>
<body>
<form id="form1">
 <div>  The user name :
  <input type="text" ID="txtUserName" onblur="GetPwdAndChk()">
  <input type="password" ID="txtPassword">
   Password: 
  <input type="checkbox" ID="chkRememberPwd" />
   Remember the password 
  <input type="button" OnClick="SetPwdAndChk()" value=" Enter the "/>
 </div>
</form>
</body>
</html>


Related articles: