1. Go to this page and download the library: Download snipershady/ratelimiter 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/ */
snipershady / ratelimiter example snippets
use Predis\Client;
use RateLimiter\Enum\CacheEnum;
use RateLimiter\Service\AbstractRateLimiterService;
class Foo(){
public function controllerYouWantToRateLimit(): Response {
$limiter = AbstractRateLimiterService::factory(CacheEnum::APCU);
$key = __METHOD__; //Name of the function you want to rate limit. You can set a custom key. It's a String!
$limit = 2; //Maximum attempts before the limit
$ttl = 3; //The timeframe you want to limit access for
if($limiter->isLimited($key, $limit, $ttl)){
throw new Exception("LIMIT REACHED: YOOUUU SHALL NOOOOT PAAAAAAASSS");
}
// ... other code
}
}
class Foo(){
public function controllerYouWantToRateLimit(): Response {
$serverIp = "192.168.0.100"; //The server where you've installed the Redis instance.
$redis = new Client("tcp://$serverIp:6379?persistent=redis01"); // Example with persistent connection.
$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $redis);
$key = __METHOD__; //Name of the function you want to rate limit. You can set a custom key. It's a String!
$limit = 2; //Maximum attempts before the limit
$ttl = 3; //The timeframe you want to limit access for
if($limiter->isLimited($key, $limit, $ttl)){
throw new Exception("LIMIT REACHED: YOOUUU SHALL NOOOOT PAAAAAAASSS");
}
// ... other code
}
}
class Foo(){
public function controllerYouWantToRateLimit(): Response {
$serverIp = "192.168.0.100"; //The server where you've installed the Redis instance.
$redis = new Client("tcp://$serverIp:6379?persistent=redis01"); // Example with persistent connection.
$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $this->redis);
$key = __METHOD__; // Name of the function you want to rate limit. You can set a custom key. It's a String!
$limit = 1; // Maximum attempts before the limit
$maxAttempts = 3; // Max number of attempts you want to allow in a timeframe
$banTimeFrame = 4; // Timeframe where maxAttempts should not be reached to avoid the ban
$ttl = 2; // The base timeframe you want to limit access for
$banTtl = 4; // If a limit is reached greater equals time of max attempts, the new timeframe limit will be 4 seconds
$clientIp = filter_input(INPUT_SERVER, 'REMOTE_ADDR'); // It is recommended to send the client IP to limit access to a function to a specific address, not to everyone
if($limiter->isLimitedWithBan($key, $limit, $ttl, $maxAttempts, $banTimeFrame, $banTtl, $clientIp))){
throw new Exception("LIMIT REACHED: YOOUUU SHALL NOOOOT PAAAAAAASSS");
}
// ... other code
}
}
bash
apt-get install php8.3-redis php8.3-apcu
# you can install php-redis and php-apcu module for the version you've installed on the system
# min version
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.