1. Go to this page and download the library: Download trukes/php-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/ */
trukes / php-rate-limiter example snippets
use Trukes\PhpRateLimiter\PhpRateLimiter;
use Psr\SimpleCache\CacheInterface;
use Trukes\PhpRateLimiter\Support\Config;
use Trukes\PhpRateLimiter\Exception\LimitExceededException;
// Example with a PSR-16 compatible cache implementation
$cache = // Your cache implementation (e.g., Redis, file-based cache, etc.)
$config = new Config();
// Instantiate the rate limiter
$rateLimiter = new PhpRateLimiter($cache, $config);
$key = 'user_123'; // Custom key (e.g., user ID, IP, etc.)
try {
$rateLimiter->hit($key); // Increment the hit count
} catch (LimitExceededException $e) {
echo 'Limit exceeded: ' . $e->getMessage();
}
// Reset the counter for a key
$rateLimiter->reset($key);
new Config(
prefix: 'your-prefix',
maxRequestsByTimeframe: 10,
timeframeInSeconds: 1
);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.