限制使用者頻繁提交

流火行者發表於2018-10-12
    private static function allowAvailRoom()
    {
        session_start();
        $now = time();
        $minute_interval = 5;
        $max_cnt = 20;
        if (isset($_SESSION['request_time'])) {
            $request_time = $_SESSION['request_time'];
            if ($now - $request_time < $minute_interval * 60) {
                if (isset($_SESSION['request_cnt'])) {
                    $request_cnt = $_SESSION['request_cnt'];
                    if ($request_cnt >= $max_cnt) {
                        return false;
                    } else {
                        $_SESSION['request_cnt'] += 1;
                        return true;
                    }
                }
            }
        }
        $_SESSION['request_time'] = $now;
        $_SESSION['request_cnt'] = 1;
        return true;
    }

5分鐘最多提交20次

相關文章