PHP code example of dino-ma / php-speed-limit

1. Go to this page and download the library: Download dino-ma/php-speed-limit 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/ */

    

dino-ma / php-speed-limit example snippets



SpeedLimit\RateLimit;
use \Predis\Client;


$redis = new Client([
    'scheme' => env('redis_scheme', 'tcp'),
    'host'   => env('redis_host', 'redis'),
    'port'   => env('redis_port', 6379),
]);
$rate = new RateLimit(15, RateLimit::MINUTE, $redis);
$rate->setRedisKey('123');


for ($i = 1; $i < 100; $i++) {
    $is = $rate->rate();
    if (!$is) {
        echo 'not allow[' . $i . ']' . PHP_EOL;
    } else {
        echo 'ok' . '[' . $i . ']' . PHP_EOL;
    }
}