借助 Rides 实现输入密码错误限制次数

2018-07-06  本文已影响13人  冰镇苏打水

借助 Rides 实现输入密码错误限制次数

验证密码前逻辑

function passwordErrorCount($userNo)
{
    $redisHost = '127.0.0.1';
    $redisProt = 6379;
    $redisPwd = 'password';
    $redis = new Redis();

    $redis->connect($redisHost, $redisProt);
    $redis->auth($redisPwd);

    $key = 'userNo:' . $userNo . 'password_error_count';
    list($time, $count) = explode(':', $redis->get($key));

    $ban = $time + 86400;
    
    $result = [
        'status' => true,
        'message' => "无限制"
    ];
        
    if (time() <= $ban && $count >= 6) {
        $result = [
            'status' => false,
            'message' => "登录失败", "登录操作被限制,请在" . date('Y-m-d H:i:s', $ban) . "后重试"
        ];
    }
    return $result;
}

验证密码

function auth()
{
    $user_no = 'U-test';
    $pwd = '123456';

    $result = passwordErrorCount($user_no);

    // 判断是否有登录限制
    if($result['status'] === false){    
        $message = $result['message'];
    }elseif ($pwd === '******') {
        $message = '登录成功';
    }else{
        $message = '密码错误';
        restrict($user_no);
    }
}

输入密码错误后逻辑

function restrict($userNo)
{
    $redisHost = '127.0.0.1';
    $redisProt = 6379;
    $redisPwd = 'password';
    $redis = new Redis();

    $redis->connect($redisHost, $redisProt);
    $redis->auth($redisPwd);

    $key = 'userNo:' . $userNo . 'password_error_count';

    list($time, $count) = explode(':', $redis->get($key));

    // 判断是否在一小时内多次出错

    $value = implode(':', array(time(), 1));
    if (date('YmdH', $time) === date('YmdH', time())) {
        $value = implode(':', array(time(), $count + 1));
    }
    $redis->set($key, $value);
}

    // 判断是否有登录限制
    $message = '密码错误';
    if($result['status'] === false){    
        $message = $result['message'];
    }elseif ($pwd === '******') {
        $message = '登录成功';
    }
上一篇下一篇

猜你喜欢

热点阅读