php 實現密碼錯誤三次鎖定賬號10分鍾


    /**
     * 登錄
     * 1、接收數據
     * 2、正則判斷接收到的數據是否合理
     * 3、根據用戶名獲取用戶數據
     *      獲取到數據 -> 繼續執行
     *      沒有獲取到數據 -> 提示:用戶名密碼錯誤
     * 4、判斷鎖定時間
     *      當前時間和鎖定時間差 大於 10分鍾 或者 沒有鎖定時間 -> 繼續執行
     *      當前時間和鎖定時間差 小於 10分鍾 -> 提示:賬號鎖定中、請10分鍾后再試
     * 5、判斷密碼
     *      == 
     *          次數=0
     *          登錄成功
     *      != 
     *          次數 大於等於 2 -> 鎖定操作、次數=0  -> 賬號已經鎖定
     *          次數 小於 2  次數+1 -> 賬號密碼錯誤
     */

    public function login()
    {
        $name = request()->post('name', '');
        $pwd = request()->post('pwd', '');
        if ( $name == '' || $pwd == '' || $name == null || $pwd == null) {
            $arr['code'] = 1;
            $arr['msg'] = '參數錯誤、用戶名或密碼不能為空';
            $arr['data'] = [];
            return json($arr);
        }

        $preg_name = '/^[\x{4e00}-\x{9fa5}]{2,5}$/u';
        if( !preg_match( $preg_name, $name ) )
        {
            $arr['code'] = 1;
            $arr['msg'] = '用戶名要求必須是2到5位的漢字';
            $arr['data'] = [];
            return json($arr);
        }

        $preg_pwd = '/^\S{5,18}$/';
        if (!preg_match($preg_pwd, $pwd)) {
            $arr['code'] = 1;
            $arr['msg'] = '密碼要求必須5到18位非空字符串';
            $arr['data'] = [];
            return json($arr);
        }

        $where['user_name'] = $name;
        $res = Db::table('user')->field('user_id,user_name,user_pwd_login,user_lock_time,user_pwd_num')->where($where)->find();
        if (!$res) {
            $arr['code'] = 1;
            $arr['msg'] = '用戶名或密碼錯誤、請重試';
            $arr['data'] = [];
            return json($arr);
        }

        if($res['user_lock_time'] != '' && time() - strtotime($res['user_lock_time']) < 1*60 ) 
        {
            $arr['code'] = 1;
            $arr['msg'] = '該賬號已被鎖定、請10分鍾后重試';
            $arr['data'] = [];
            return json($arr);
        }

        $upd_where['user_id'] = $res['user_id'];
        if( $pwd != $res['user_pwd_login'] )
        {
            // 次數 大於等於 2 -> 鎖定操作、次數=0 -> 賬號已經鎖定
            if( $res['user_pwd_num'] >= 2 )
            {
                $upd_data['user_lock_time'] = date('Y-m-d H:i:s', time() );
                $upd_data['user_pwd_num'] = 0;
                Db::table('user')->where($upd_where )->update( $upd_data );
                $arr['code'] = 1;
                $arr['msg'] = '賬號密碼錯誤次數超過3次、賬號鎖定10分鍾、請稍后重試';
                $arr['data'] = [];
                return json($arr);
            }
            else
            {
                // 次數 小於2 次數+1 -> 賬號密碼錯誤
                Db::table('user')->where($upd_where)->setInc('user_pwd_num');
                $arr['code'] = 1;
                $arr['msg'] = '賬號密碼錯誤、剩余'. (3 - ($res['user_pwd_num'] + 1) ) .'次、請稍后重試';
                $arr['data'] = [];
                return json($arr);
            }
        }

        Db::table('user')->where($upd_where)->update(['user_pwd_num'=>0]);
        
        Session::set('user', $res);
        $arr['code'] = 0;
        $arr['msg'] = '登錄成功';
        $arr['data'] = $res;
        return json($arr);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM