记住密码?so easy!

            哇哦!今天心情飞一般的感觉!趁此大好时机,赶快闲下来写一下自己的博客。

            虽然工作了有一段时间了,但由于工作的原因,也没有抽出一点时间来写博客。在一帮朋友的“”怂恿“”下,自己也开始了,毕竟是初学者,写的不好,还请大家伙见谅!刚开始嘛!先上点儿“”小菜“”!

           今天呢我主要是想讲述一下在登录过程中的记住密码问题,对于一些初写者来说,记住密码是个问题,今天在公司里,有个新同事来问我这样一个问题,说记住密码怎么做?听到这话时,我感觉有点震惊!后来想了一想,毕竟还有点嫩,也很正常啊!我在这也简单说一下。

         记住密码的核心就是当用户将记住密码这个框选中,在验证用户登录成功时,将用户的信息放进cookie中,切记是cookie,但有很多人以为是放在session中,这一点要纠正大家。当用户再次访问浏览器,只要输入用户名后,失去焦点时触发一个事件,将用户名所对应的信息从cookie中提取出来,与之进行对比,如果有这个用户,将记住密码这个框回显,密码的内容重新赋给密码框,就是这么简单,废话不多说!看下代码:


首先这些是js的东西:

//再次登录时,写完用户名之后失去焦点后触发的事件:

$("input",$("#txtUserName").next("span")).blur(function(){
                var usr = document.getElementById('txtUserName').value;
                var pwd = GetCookie(usr);
                if (pwd != null) {
                    document.getElementById('chkRememberPwd').checked = true;
                    $("#txtPassword").textbox("setValue",pwd);
                } else {
                    document.getElementById('chkRememberPwd').checked = false;
                     $("#txtPassword").textbox("setValue","");
                }
            
      })


window.οnlοad=function onLoginLoaded() {
            if (isPostBack == "False") {
                GetLastUser();
            }
     }
     
    function GetLastUser() {
        var id = "e10adc3949ba59abbe56e057f20f883e";//GUID标识符
        var usr = GetCookie(id);
        if (usr != null) {
            document.getElementById('txtUserName').value = usr;
        } else {
            document.getElementById('txtUserName').value = "001";
        }
        GetPwdAndChk();
    }
    
     
    function SetLastUser(usr) {
        var id = "e10adc3949ba59abbe56e057f20f883e";
        var expdate = new Date();
        //当前时间加上两周的时间
        expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
        SetCookie(id, usr, expdate);
    }
   
    //取Cookie的值
    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 = "false";
    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1) endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }
    //写入到Cookie
     
    function SetCookie(name, value, expires) {
        var argv = SetCookie.arguments;
        //本例中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);
    }


这些是登录时将用户名和密码放进cookie中:

                //取用户名
                var usr = document.getElementById('txtUserName').value;
                //将最后一个用户信息写入到Cookie
                SetLastUser(usr);
                //如果记住密码选项被选中
                if (document.getElementById('chkRememberPwd').checked == true) {
                    //取密码值
                    var pwd = document.getElementById('txtPassword').value;
                    var expdate = new Date();
                    expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
                    //将用户名和密码写入到Cookie
                    SetCookie(usr, pwd, expdate);
                } 


最后是HTML的一些东西:

<div class="easyui-panel" title="欢迎登录" style="width:100%;max-width:400px;padding:30px 60px;" align="center">
        <form id="ff" method="post">
            <div style="margin-bottom:20px" >
                <input class="easyui-textbox"  name="username" id="txtUserName" οnblur="GetPwdAndChk()" style="width:100%" data-options="label:'用户名:',required:true">
                <span></span>
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="password" id="txtPassword" type="password" style="width:100%" data-options="label:'密码:',required:true">
                <span><input type="checkbox" ID="chkRememberPwd"> 记住密码</span>
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="code"  style="width:100%" data-options="label:'验证码:',required:true">
                <img id="showCode" src="<%=request.getContextPath() %>/css/image.jsp">
                  <a id="aaa" href = "javascript:void(0)" style="color: black"><strong>换一张</strong></a>
                  <span id="msg_code"></span>
            </div>
        </form>

      代码已经写完了!在这里给大家提醒一下!遇到问题时不要着急,冷静下来,深度剖析这个问题的原理,很快就有解决的方案,加油!少年

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值