Gt199 2023-04-15 22:09 采纳率: 83.3%
浏览 18
已结题

如何用php实现登陆次数限制

以教务系统学生端登陆为例,登陆错误限制5次,超过10分钟内无法登陆,该如何实现?

  • 写回答

1条回答 默认 最新

  • CQ.abc 2023-04-15 22:11
    关注
    session_start(); // 开启 Session
    
    $max_attempts = 5; // 最多尝试次数
    $block_duration = 10 * 60; // 阻塞时间(单位:秒)
    
    if (isset($_SESSION['attempts'])) {
        $attempts = $_SESSION['attempts'];
    } else {
        $attempts = 0;
    }
    
    if (isset($_SESSION['last_attempt_time'])) {
        $last_attempt_time = $_SESSION['last_attempt_time'];
    } else {
        $last_attempt_time = 0;
    }
    
    if ($attempts >= $max_attempts && time() - $last_attempt_time < $block_duration) {
        $remaining_time = $block_duration - (time() - $last_attempt_time);
        echo '您已经尝试了 ' . $attempts . ' 次,请在 ' . $remaining_time . ' 秒后再试。';
    } else {
        // 处理登录逻辑
        if ($login_successful) {
            // 登录成功,清空记录
            $_SESSION['attempts'] = 0;
            $_SESSION['last_attempt_time'] = 0;
        } else {
            // 登录失败,记录次数和时间
            $_SESSION['attempts'] = $attempts + 1;
            $_SESSION['last_attempt_time'] = time();
            echo '账号或密码错误。';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 创建了问题 4月15日

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化