php 實現密碼錯誤三次鎖定賬號10分鐘
發布時間:2023-10-07
/** * 登錄 * 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}]{25}$/u'; if( !preg_match( $preg_name $name ) ) { $arr['code'] = 1; $arr['msg'] = '用戶名要求必須是2到5位的漢字'; $arr['data'] = []; return json($arr); } $preg_pwd = '/^S{518}$/'; 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_iduser_nameuser_pwd_loginuser_lock_timeuser_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); }
標簽: php登錄 , php密碼錯誤三次鎖定 , php鎖定賬號 ,