打印 上一主题 下一主题

[百度网盘]JS读取cookie(记住账号密码)

[复制链接]
跳转到指定楼层
楼主
admin 发表于 2017-10-2 07:08:24
3967 0

很多登录功能上都有个“记住密码”的功能,其实无非就是对cookie的读取。

下面展示这个功能的代码,原作者已无法考究。。。。

测试方法:直接输入账号密码,提交后,刷新页面,再输入同样的账号,就可以显示

  1. <!DOCTYPE HTML>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>js COOKIE 记住帐号或密码</title>
  5. <script type="text/javascript">
  6.     window.onload=function onLoginLoaded() {
  7.         if (isPostBack == "False") {
  8.             GetLastUser();
  9.         }
  10.     }
  11.     function GetLastUser() {
  12.         var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID标识符
  13.         var usr = GetCookie(id);
  14.         if (usr != null) {
  15.             document.getElementById('txtUserName').value = usr;
  16.         } else {
  17.             document.getElementById('txtUserName').value = "001";
  18.         }
  19.         GetPwdAndChk();
  20.     }
  21.     //点击登录时触发客户端事件
  22.     function SetPwdAndChk() {
  23.         //取用户名
  24.         var usr = document.getElementById('txtUserName').value;
  25.         alert(usr);
  26.         //将最后一个用户信息写入到Cookie
  27.         SetLastUser(usr);
  28.         //如果记住密码选项被选中
  29.         if (document.getElementById('chkRememberPwd').checked == true) {
  30.             //取密码值
  31.             var pwd = document.getElementById('txtPassword').value;
  32.             alert(pwd);
  33.             var expdate = new Date();
  34.             expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
  35.             //将用户名和密码写入到Cookie
  36.             SetCookie(usr, pwd, expdate);
  37.         } else {
  38.             //如果没有选中记住密码,则立即过期
  39.             ResetCookie();
  40.         }
  41.     }
  42.     function SetLastUser(usr) {
  43.         var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
  44.         var expdate = new Date();
  45.         //当前时间加上两周的时间
  46.         expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
  47.         SetCookie(id, usr, expdate);
  48.     }
  49.     //用户名失去焦点时调用该方法
  50.     function GetPwdAndChk() {
  51.         var usr = document.getElementById('txtUserName').value;
  52.         var pwd = GetCookie(usr);
  53.         if (pwd != null) {
  54.             document.getElementById('chkRememberPwd').checked = true;
  55.             document.getElementById('txtPassword').value = pwd;
  56.         } else {
  57.             document.getElementById('chkRememberPwd').checked = false;
  58.             document.getElementById('txtPassword').value = "";
  59.         }
  60.     }
  61.     //取Cookie的值
  62.     function GetCookie(name) {
  63.         var arg = name + "=";
  64.         var alen = arg.length;
  65.         var clen = document.cookie.length;
  66.         var i = 0;
  67.         while (i < clen) {
  68.             var j = i + alen;
  69.             //alert(j);
  70.             if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
  71.             i = document.cookie.indexOf(" ", i) + 1;
  72.             if (i == 0) break;
  73.         }
  74.         return null;
  75.     }
  76.     var isPostBack = "<%= IsPostBack %>";
  77.     function getCookieVal(offset) {
  78.         var endstr = document.cookie.indexOf(";", offset);
  79.         if (endstr == -1) endstr = document.cookie.length;
  80.         return unescape(document.cookie.substring(offset, endstr));
  81.     }
  82.     //写入到Cookie
  83.     function SetCookie(name, value, expires) {
  84.         var argv = SetCookie.arguments;
  85.         //本例中length = 3
  86.         var argc = SetCookie.arguments.length;
  87.         var expires = (argc > 2) ? argv[2] : null;
  88.         var path = (argc > 3) ? argv[3] : null;
  89.         var domain = (argc > 4) ? argv[4] : null;
  90.         var secure = (argc > 5) ? argv[5] : false;
  91.         document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
  92.     }
  93.     function ResetCookie() {
  94.         var usr = document.getElementById('txtUserName').value;
  95.         var expdate = new Date();
  96.         SetCookie(usr, null, expdate);
  97.     }
  98. </script>
  99. </head>
  100. <body>
  101. <form id="form1">
  102.   <div> 用户名:
  103.     <input type="text" ID="txtUserName" onblur="GetPwdAndChk()">
  104.     <input type="password" ID="txtPassword">
  105.     密码:
  106.     <input type="checkbox" ID="chkRememberPwd" />
  107.     记住密码
  108.     <input type="button" OnClick="SetPwdAndChk()" value="进入"/>
  109.   </div>
  110. </form>
  111. </body>
  112. </html>
复制代码


吾爱编程网 - 免责声明
1、吾爱编程网为非营利性网站,全站所有资料仅供网友个人学习使用,禁止商用
2、本站所有文档、视频、书籍等资料均由网友分享,本站只负责收集不承担任何技术及版权问题
3、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除下载链接并致以最深的歉意
4、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
5、一经注册为本站会员,一律视为同意网站规定,本站管理员及版主有权禁止违规用户
6、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和吾爱编程网的同意
7、吾爱编程网管理员和版主有权不事先通知发贴者而删除本文




上一篇:Javascript检测浏览器类型和版本的代码(兼容ie11)
下一篇:JS数学揭密之三角函数视频教程讲义源码 7课
收藏
收藏
支持
支持
反对
反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部 返回列表

平台简介

吾爱编程网:http://www.52pg.net/是IT技能学习交流平台,我们提供了丰富的移动端开发、php开发、web前端开发、android开发、Java开发、Python开发、大数据开发、区块链开发、人工智能开发以及html5等大量的实战视频教程资源。(如果我们有侵犯了您权益的资源请联系我们删除)

点击这里给我发消息|Archiver|手机版|小黑屋|站点地图|吾爱编程  |网站地图

Powered by Discuz! X3.2??? 2017-2020 Comsenz Inc.??吾爱编程网