php-ratelimiter icon indicating copy to clipboard operation
php-ratelimiter copied to clipboard

Call the rate limiter many times in the same php code with different limits

Open leonardoxc opened this issue 9 years ago • 0 comments

With this branch we can do this:

$memcache = new Memcache;
$memcache->connect('127.0.0.1',11211);

$rateLimiter = new RateLimiter($memcache, $_SERVER["REMOTE_ADDR"]);
try {
    // 10 requests / minute
    $rateLimiter->limitRequestsInMinutes(10, 1);
} catch (RateExceededException $e) {
    header("HTTP/1.0 529 Too Many Requests");
    exit;
}

try {
    // 30 requests / 5 minute
    $rateLimiter->limitRequestsInMinutes(30, 5);
} catch (RateExceededException $e) {
    header("HTTP/1.0 529 Too Many Requests");
    exit;
}

etc....

leonardoxc avatar Sep 03 '16 11:09 leonardoxc