侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

基于localStorge开发登录模块的记住密码与自动登录实例

技术  /  管理员 发布于 8年前   524

关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个哥们说有私活,开发一个****模块,我那天手痒痒就和他聊了两句,然后,就决定给她做这个模块了,和他谈了谈交付时间,他说最迟两天,然后谈了谈加个,最后达成,500¥!!!这个模块其实第一天晚上我就开发出来了,那时我给他微信说,功能模块开发ok了,要不要远程查看一下,没问题的话就交了,一会他回我,好了就发过来,然后微信就转过来500¥,当时很诧异,毕竟是处女秀,然后就把项目交给他了,并且是完美交付,在客户那里,也没有出现问题!到如今想想,还激动啊!记录那个时刻--2016-3。

摘要:传动的记住密码与自动登录模块,都是基于cookie,但是cookie上做的话,有一些弊端,鸟看了就是cookie文件大小受限,所以本问叙述的是基于H5上的storge,本地持久化存储来做的自动登录和记住密码的,所以如果你不懂storge的话,建议先去充电!

充电:了解localstorge

备注:这是一个仿网页知乎的登录模块,如果想要完整源码,可以联系鸟哦

效果图:

核心源码分享:

<!DOCTYPE html><html lang="en"><head>  <meta charset="utf-8">  <title>登录 - 仿知乎 - Thousands Find</title>  <link rel="stylesheet" type="text/css" href="https:/article/style/register-login.css" rel="external nofollow" >  <script type="text/javascript" src="https:/article/js/jquery.min.js"></script>  <script>    $(document).ready(function () {      //读取 localStage 本地存储,填充用户名密码,如果自动登录有值直接跳转;        //相反,跳转到本页面,等待登陆处理        var storage = window.localStorage;      var getEmail = storage["email"];      var getPwd = storage["password"];      var getisstroepwd = storage["isstorePwd"];      var getisautologin = storage["isautologin"];      if ("yes" == getisstroepwd) {        if ("yes" == getisautologin) {          if ((("" != getEmail) || (null != getEmail)) && (("" != getPwd) || (null != getPwd))) {//lacoste 已经保存 登陆信息 直接登陆   //alert('正在自动登录'); $("#email").val(getEmail);$("#password").val(getPwd);// window.location="";   //加载时显示:正在自动登录 $.ajax({  url: 'LoginServlet.ashx',  data: {    email: getEmail,    password: getPwd  },    dataType: 'json',  success: function (data) {    if (data.msg == "") {      alert("账号信息异常,请核实后重新登录");    } else {      //alert(123);      //登录成功后保存session,如果选择了记住密码,再保存到本地       window.location.href ='Default2.aspx';     }  },  error: function () {    alert("系统错误");  }});          }        }        else {          $("#email").val(getEmail);          $("#password").val(getPwd);          document.getElementById("isRemberPwdId").checked = true;        }      }    });function login() {      var userEmail = $("#email").val();      var userPassWord = $("#password").val();      if (userEmail != "" && userPassWord != "") {        var storage = window.localStorage;        //记住密码          if (document.getElementById("isRemberPwdId").checked) {          //存储到loaclStage //alert(134);          storage["email"] = userEmail;          storage["password"] = userPassWord;          storage["isstorePwd"] = "yes";        }        else {          storage["email"] = userEmail;          storage["isstorePwd"] = "no";        }        //下次自动登录          if (document.getElementById("isAutoLoginId").checked) {          //存储到loaclStage storage["email"] = userEmail;          storage["password"] = userPassWord;          storage["isstorePwd"] = "yes";          storage["isautologin"] = "yes";        }        else {          storage["email"] = userEmail;          storage["isautologin"] = "no";        }        $.ajax({          url: 'LoginServlet.ashx',          data: {"email": userEmail,"password": userPassWord          },          dataType: 'json',          success: function (data) {if (data.msg == "") {  alert("用户名或密码错误");} else {  alert("登陆成功");  //登录成功后保存session,如果选择了记住密码,再保存到本地   window.location.href = 'Default.aspx';}          },          error: function () {alert("系统错误1");          }        });        //alert("登录成功");      }      else {        alert("用户名密码不能为空");      }    }      </script></head><body>  <div id="box"></div>  <div class="cent-box">    <div class="cent-box-header">      <h1 class="main-title hide">仿知乎</h1>      <h2 class="sub-title">生活热爱分享 - Thousands Find</h2>    </div>    <div class="cont-main clearfix">      <div class="index-tab">        <div class="index-slide-nav">          <a href="https:/article/login.html" rel="external nofollow" class="active">登录</a>          <a href="https:/article/register.html" rel="external nofollow" >注册</a>          <div class="slide-bar"></div>        </div>      </div>      <form id="loginform" name="loginform" autocomplete="on" method="post">        <div class="login form">          <div class="group"><div class="group-ipt email">  <input type="email" name="email" id="email" class="ipt" placeholder="邮箱地址" required/></div><div class="group-ipt password">  <input type="password" name="password" id="password" class="ipt" placeholder="输入您的登录密码" required/></div>          </div>        </div>        <div class="button">          <button type="button" class="login-btn register-btn" id="button" onclick="login()">登录</button>        </div>        <div class="remember clearfix">          <label for="loginkeeping" class="remember-me"><input type="checkbox" name="isRemberPwdId" id="isRemberPwdId" class="remember-mecheck" checked />记住密码           </label>          <label for="autologin" class="forgot-password"><input type="checkbox" name="isAutoLoginId" id="isAutoLoginId" class="remember-mecheck" checked />自动登录           </label>        </div>      </form>    </div>  </div>  <div class="footer">    <p>仿知乎 - Thousands Find</p>    <p>copy@*.* 2016</p>  </div>  <script src='https:/article/js/particles.js' type="text/javascript"></script>  <script src='https:/article/js/background.js' type="text/javascript"></script>  <script src='https:/article/js/jquery.min.js' type="text/javascript"></script>    <script src='https:/article/js/layer/layer.js' type="text/javascript"></script>  <script src='https:/article/js/index.js' type="text/javascript"></script></body></html>

最后总结一下:

这个模块是通用的,我们要做的是:

1.当用户点击登录的时候,首先拿到表单里的数据
2.做出判断,判断用户是否勾选记住密码 或者 自动登录

3.都没勾选,对数据进行加密,发到服务器端做登录校验,之后返回

4.勾选了记住密码,就将用户名密码保存到storge,核心代码赞一下

var storage = window.localStorage;        //记住密码          if (document.getElementById("isRemberPwdId").checked) {          //存储到loaclStage //alert(134);          storage["email"] = userEmail;          storage["password"] = userPassWord;          storage["isstorePwd"] = "yes";        }        else {          storage["email"] = userEmail;          storage["isstorePwd"] = "no";        }

记住,这时你已经勾选了记住密码,下次登录时,该如何操作?

在$(function (){})里,也就是浏览器渲染标签时,做出判断,看一下storge['isstorePwd']是否为yes,核心代码赞一赞

$(document).ready(function () {      //读取 localStage 本地存储,填充用户名密码,如果自动登录有值直接跳转;        //相反,跳转到本页面,等待登陆处理        var storage = window.localStorage;      var getEmail = storage["email"];      var getPwd = storage["password"];      var getisstroepwd = storage["isstorePwd"];      var getisautologin = storage["isautologin"];      if ("yes" == getisstroepwd) {        if ("yes" == getisautologin) {          ....          }        }        else {          $("#email").val(getEmail);          $("#password").val(getPwd);          document.getElementById("isRemberPwdId").checked = true;        }      }    });

ok 如果记住密码就搞定了!

5.自动登录:这个功能还用我说吗?和记住密码类似!

//下次自动登录          if (document.getElementById("isAutoLoginId").checked) {          //存储到loaclStage storage["email"] = userEmail;          storage["password"] = userPassWord;//密码存到storage里          storage["isstorePwd"] = "yes";          storage["isautologin"] = "yes";        }        else {          storage["email"] = userEmail;          storage["isautologin"] = "no";        }

当用户再次登录的时候,还是在一加载的时候,做出判断,是否勾选自动登录,勾选的话,从storage里拿到数据,直接发生异步

请求,就不用用户做出点击登录事件了!

if ("yes" == getisautologin) {          if ((("" != getEmail) || (null != getEmail)) && (("" != getPwd) || (null != getPwd))) {//lacoste 已经保存 登陆信息 直接登陆   //alert('正在自动登录'); $("#email").val(getEmail);$("#password").val(getPwd);// window.location="";   //加载时显示:正在自动登录 $.ajax({  url: 'LoginServlet.ashx',  data: {    email: getEmail,    password: getPwd  },    dataType: 'json',  success: function (data) {    if (data.msg == "") {      alert("账号信息异常,请核实后重新登录");    } else {      //alert(123);      //登录成功后保存session,如果选择了记住密码,再保存到本地       window.location.href ='Default2.aspx';     }  },  error: function () {    alert("系统错误");  }});

以上这篇基于localStorge开发登录模块的记住密码与自动登录实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    CKEditor与dotnetcore实现图片上传功能
    下一条:
    VS2015 IIS Express无法启动的解决方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    • 2025-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客