JavaScript实现记住密码,采用cookie技术

朱雀 2022-03-16 09:18 304阅读 0赞

我知道用cookie保存密码,容易造成密码的安全问题,但是我这只是自己写的小项目,权当练手了。

话不多说,直接上代码。

HTML代码如下:

  1. <div class="login-box-body">
  2. <p class="login-box-msg">Please enter your Account.</p>
  3. <form action="" method="post">
  4. <div class="form-group has-feedback">
  5. <input type="text" name="username" id="username" autocomplete="off" class="form-control" placeholder="请输入学号">
  6. <span class="glyphicon glyphicon-user form-control-feedback"></span>
  7. </div>
  8. <div class="form-group has-feedback">
  9. <input type="password" name="password" id="password" class="form-control" placeholder="请输入密码">
  10. <span class="glyphicon glyphicon-lock form-control-feedback"></span>
  11. </div>
  12. <p id="error-msg"></p>
  13. <div class="row">
  14. <div class="col-xs-8">
  15. <div class="checkbox icheck">
  16. <label>
  17. <input type="checkbox" id="remember_password"> 记住密码
  18. </label>
  19. </div>
  20. </div>
  21. <!-- /.col -->
  22. <div class="col-xs-4">
  23. <button type="submit" id="submit" class="btn btn-primary btn-block btn-flat">登 录</button>
  24. </div>
  25. <!-- /.col -->
  26. </div>
  27. </form>
  28. </div>

不要忘了在body里加上如下代码:

  1. <body onLoad="document.getElementById('username').focus();GetCookie();">

表示只要页面一加载,首先将鼠标的焦点给用户名输入框,并且执行GetCookie()函数。。。

最后加上JS代码:

  1. $("#submit").click(function () {
  2. console.log("$(input[name='username']).val()", $("input[name='username']").val());
  3. console.log("$(input[name='username']).val()", $("input[name='password']").val());
  4. if (checkLogin()) {
  5. $.ajax({
  6. type: "POST",
  7. url: _ctx + "/login",
  8. data: {
  9. username: $("input[name='username']").val(),
  10. password: $("input[name='password']").val(),
  11. time: new Date().getTime()
  12. },
  13. dataType: "json",
  14. cache: false,
  15. success: function (data) {
  16. if ("success" == data.status) {
  17. window.location.href = _ctx + "/index";
  18. } else {
  19. $("#error-msg").html(data.msg);
  20. }
  21. }
  22. })
  23. }
  24. return false;
  25. });
  26. function checkLogin() {
  27. if ($("input[name='username']").val() == "") {
  28. alert("username cannot be empty")
  29. $("input[name='username']").focus();
  30. return false;
  31. }
  32. if ($("input[name='password']").val() == "") {
  33. alert("password cannot be empty")
  34. $("input[name='password']").focus();
  35. return false;
  36. }
  37. else {
  38. saveInfo();
  39. return true;
  40. }
  41. }
  42. saveInfo = function () {
  43. try {
  44. var isSave = document.getElementById('remember_password').checked; //保存按键是否选中
  45. if (isSave) {
  46. var username = document.getElementById('username').value;
  47. var password = document.getElementById('password').value;
  48. if (username != "" && password != "") {
  49. SetCookie(username, password);
  50. }
  51. } else {
  52. SetCookie("", "");
  53. }
  54. } catch (e) {
  55. }
  56. }
  57. function SetCookie(username, password) {
  58. var Then = new Date();
  59. Then.setTime(Then.getTime() + 1866240000000);
  60. document.cookie = "username=" + username + "%%" + password + ";expires=" + Then.toGMTString();
  61. }
  62. function GetCookie() {
  63. var nmpsd;
  64. var nm;
  65. var psd;
  66. var cookieString = new String(document.cookie);
  67. var cookieHeader = "username=";
  68. var beginPosition = cookieString.indexOf(cookieHeader);
  69. cookieString = cookieString.substring(beginPosition);
  70. var ends = cookieString.indexOf(";");
  71. if (ends != -1) {
  72. cookieString = cookieString.substring(0, ends);
  73. }
  74. if (beginPosition > -1) {
  75. nmpsd = cookieString.substring(cookieHeader.length);
  76. if (nmpsd != "") {
  77. beginPosition = nmpsd.indexOf("%%");
  78. nm = nmpsd.substring(0, beginPosition);
  79. psd = nmpsd.substring(beginPosition + 2);
  80. document.getElementById('username').value = nm;
  81. document.getElementById('password').value = psd;
  82. if (nm != "" && psd != "") {
  83. // document.forms[0].checkbox.checked = true;
  84. document.getElementById('remember_password').checked = true;
  85. }
  86. }
  87. }
  88. }

到此,我们采用cookie技术用JavaScript实现记住密码就完成了,最后我们可以用chrome浏览器的开发者工具看到,我们的用户名和密码已经保存到cookies里面去了。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5MzMxNzEz_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,304人围观)

还没有评论,来说两句吧...

相关阅读