gpt4 book ai didi

php - 使用时间戳限制错误登录尝试的次数

转载 作者:行者123 更新时间:2023-11-29 07:26:19 26 4
gpt4 key购买 nike

在过去 15 分钟内 4 次登录尝试失败后,让用户验证验证码的正确方法是什么?我一切都运行良好,但不是查询的时间戳部分。更具体地说,当用户尝试登录 5 次左右时,无论是否经过 15 分钟或 30 分钟,它都可以在 4 次失败尝试后显示验证码...

 $query1 = "SELECT login_attempts from users WHERE email = '$email' AND last_login < NOW() - INTERVAL 15 MINUTE";
$result1 = mysqli_query($dbc, $query1) OR die(mysqli_error());
$row = mysqli_fetch_array($result1);
$fail = (int)$row['login_attempts'];

最佳答案

如果我的理解是正确的,您必须检查最后 4 次失败的登录尝试是否是在 15 分钟之前。

为了实现此目的,您必须将最近四次不成功登录的时间戳存储在数据库中。

  1. 在数据库中创建一个名为 unsuccesful_login_timestamps 的字段,作为文本或大尺寸的 varchar。我们将在此字段中以逗号分隔的形式存储最近四次不成功登录的 UNIX 时间戳。
  2. 当用户尝试登录时,实现以下逻辑

    如果用户名和密码有效,则让用户登录(如果您愿意,如果登录成功,您可以清除 unsuccesful_login_timestamps 字段)。否则,运行以下代码。

    $last_login_string = {{ get unsuccesful_login_timestamps value for this user from database }}
    $last_login_string = update_last_login($last_login_string);
    $fourth_last_login = get_4th_last_login($last_login_string);
    $time_difference = time() - $fourth_last_login;

    {{Update unsuccesful_login_timestamps in db with $last_login_string}}

    if($time_difference <900){
    //show captcha
    }else{
    //no_need_for_captcha
    }
    //Method to update last 4 unsuccessful logins by removing
    // the last one from the starting and append the latest time in the end
    function update_last_login($last_login_string){
    $time_array = array();
    if(strlen($last_login_string) > 0){
    $time_array = explode(",",$last_login_string);
    $size = count($time_array);
    if($size ==0){ //first attempt
    $last_login_string = time();
    }else if($size == 4){ //>=4th attempt
    $time_array[4]=time();
    array_shift($time_array);
    $last_login_string = implode(",",$time_array);
    }else{ // >0, but <4 attempts
    $time_array[$size]=time();
    $last_login_string = implode(",",$time_array);
    }
    return $last_login_string;
    }else{
    return time();
    }
    }

    function get_4th_last_login($last_login_string){
    $time_array = array();
    $time_array = explode(",",$last_login_string);
    if($size !=4){
    $last_login_time time();
    }else{
    $last_login_time = $time_array[0];
    }
    return $last_login_time;
    }

关于php - 使用时间戳限制错误登录尝试的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34407541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com