PHP code example of antikirra / rate-limiter

1. Go to this page and download the library: Download antikirra/rate-limiter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antikirra / rate-limiter example snippets




use Antikirra\RateLimiter;

nect('127.0.0.1');

// maximum of three requests per minute
$limiter = new RateLimiter($redis, "signin_by_ip{$_SERVER['REMOTE_ADDR']}", 3, 60, 0.75);

$result = $limiter->check(); // returns the actual counter value without any side effects
echo $result->getCount(); // (int) actual counter value
print_r($result->isPassed()); // (bool) true - it's okay, false - flood has been detected!!!

$result = $limiter->hit(); // increments the counter value, returning the result of the check
echo $result->getCount(); // (int) updated counter value
print_r($result->isPassed()); // (bool) true - it's okay, false - flood has been detected!!!
print_r($result->isFailed()); // (bool) true - flood has been detected!!!, false - it's okay

$limiter->reset(); // (void) resets counters to zero