登录验证( cookie三次锁定)

思路

1.接受值判断是否为空

2、登录错误大于三次时,判断时间是否过期

3、判断登录的错误次数

4.查询数据判断是否成功,不成功次数+1,大于三次设置cookie给个过期时间(方便上面的调用)

登录的表单页面

<form action="{:url('login/login')}" method="post">
            <table border="0" style="width:370px; font-size:14px; margin-top:30px;" cellspacing="0" cellpadding="0">
              <tr height="50" valign="top">
              	<td width="55">&nbsp;</td>
                <td>
                	<span class="fl" style="font-size:24px;">登录</span>
                    <span class="fr">还没有商城账号,<a href="{:url('login/regist')}" style="color:#ff4e00;">立即注册</a></span>
                </td>
              </tr>
              <tr height="70">
                <td>用户名</td>
                <td><input type="text" name="username" value="" class="l_user" /></td>
              </tr>
              <tr height="70">
                <td>密&nbsp; &nbsp; 码</td>
                <td><input type="password" name="pwd" value="" class="l_pwd" /></td>
              </tr>
              <tr>
              	<td>&nbsp;</td>
                <td style="font-size:12px; padding-top:20px;">
                	<span style="font-family:'宋体';" class="fl">
                    	<label class="r_rad"><input type="checkbox" /></label><label class="r_txt">请保存我这次的登录信息</label>
                    </span>
                    <span class="fr"><a href="#" style="color:#ff4e00;">忘记密码</a></span>
                </td>
              </tr>
              <tr height="60">
              	<td>&nbsp;</td>
                <td><input type="submit" value="登录" class="log_btn" /></td>
              </tr>
            </table>
            </form>

控制器页面

  //立即登录
    public function login()
    {
        if(Request::instance()->isGet()){
            return view('login');
        }else {

            $arr=input('post.');
            //用验证器进行验证(错误信息,是否为空的验证)
            $valuedate=new Validate([
                'username'=>'require',    //require信息不能为空的提示
                'pwd'=>'require',
            ]);
            if(!$valuedate->check($arr)){
                $error=$valuedate->getError();   //提示的信息(提示语)
                $this->error($error,'login/login');
            }


            //先验证是否错误三次
            if(Cookie::has('count')){
                if(Cookie::get('count')>=3){
                    $times=Cookie::get('times');  //获取时间(下面cookie里的)
                    //判断两个时间差多少,相差多少的就是剩下多少秒
                    //因为下面设置时间戳加60,所以要用下面的减去当前的时间戳
                    $times=$times-time();
                    //判断时间,用来判断多久清除cookie的
                    if($times<=0){//锁定时间过期了
                        //清空次数
                        Cookie::clear('count');
                    }else{
                        $this->error('以登录失败三次,还有'.$times.'秒');
                    }
                }
            }


            //将密码进行md5加密,否则语数据库匹配不成功
            //密码进行加密
            $arr['pwd']=md5($arr['pwd']);
            $user=new Username();
            //查新这一条数据
            $res=$user::where($arr)->find();
            if($res){
                $this->success('登录成功','index/index');
            }else{
                $count=1;
                //判断cookie里的count是够存在
                if(!Cookie::has('count')){
                    //不存在的话赋值为1
                    Cookie::set('count',1);
                }else{
                    //存在的就赋值 每次加一
                    $count=Cookie::get('count')+1;
                    Cookie::set('count',$count);
                    //当count等于3是  设置一个time。值为当前的时间+60S
                    if($count==3){
                        Cookie::set('times',time()+60);  //锁定时间 (时间为60秒)
                    }
                }
                $this->error('登录失败'.$count.'次');
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/baiyawen1/article/details/81871479