1. Go to this page and download the library: Download mata-sh/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/ */
mata-sh / rate-limiter example snippets
use MataSh\RateLimiter\RateLimiter;
use MataSh\RateLimiter\RateLimitIdentifier;
// Initialize with explicit storage backend
$limiter = new RateLimiter('apcu');
// Simple identifier
if ($limiter->allow('api_user_123', 10, 60)) {
// Process request (10 requests per 60 seconds)
}
// IP-based limiting
$identifier = RateLimitIdentifier::fromIp();
if (!$limiter->allow($identifier, 100, 3600)) {
http_response_code(429);
exit('Rate limit exceeded');
}
// IP + username combined
$identifier = RateLimitIdentifier::fromIpUser($username);
$limiter->allow($identifier, 20, 60);
// Check without consuming
$status = $limiter->check($identifier, 100, 3600);
header('X-RateLimit-Remaining: ' . $status['remaining']);
RateLimitIdentifier::fromIp(); // Auto-detect IP
RateLimitIdentifier::fromIpUser($username); // IP + username
RateLimitIdentifier::getClientIp(); // Get IP directly
// Or create custom identifiers
'user_' . $userId
'api_key_' . hash('sha256', $apiKey)
// Disable rate limiting (allows all requests)
$limiter = new RateLimiter('apcu');
$limiter->setRateLimitingEnabled(false);
// Set default limits
$limiter->setRateLimitingConfig(100, 3600);
$limiter = new RateLimiter('apcu');
use MataSh\RateLimiter\RateLimiter;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('password');
$limiter = new RateLimiter('redis', null, $redis);
$limiter = new RateLimiter('file', '/var/app/cache/rate_limit');
$limiter = new RateLimiter('session');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.