Rate Limiter

Rate Limiter

Controle de taxa de requisições por IP ou sessão.

Throttle por IP

$key = 'login:' . getClientIP();
$maxRequests = 10;
$window = 60; // segundos

if (throttle_check($key, $maxRequests, $window)) {
    // Ação permitida
    throttle_hit($key, $maxRequests, $window);
} else {
    // Limite excedido
    $remaining = throttle_remaining($key, $maxRequests, $window);
    http_response_code(429);
    echo "Aguarde {$remaining} segundos.";
}

Throttle por Sessão

$key = 'form_enviar';
$max = 5;
$window = 60;

if (throttle_hit_session($key, $max, $window)) {
    // Dentro do limite
} else {
    // Excedeu
    $remaining = throttle_remaining_session($key);
}

Funções

FunçãoDescrição
throttle_hit($key, $max, $window)Registra e verifica limite por IP
throttle_check($key, $max, $window)Verifica sem registrar
throttle_remaining($key, $max, $window)Tempo restante para reset
throttle_hit_session($key, $max, $window)Registra e verifica por sessão
throttle_remaining_session($key)Tempo restante (sessão)