1. Go to this page and download the library: Download sunspikes/php-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/ */
sunspikes / php-ratelimiter example snippets
// 1. Make a rate limiter with limit 3 attempts in 10 minutes
$cacheAdapter = new DesarrollaCacheAdapter((new DesarrollaCacheFactory())->make());
$settings = new ElasticWindowSettings(3, 600);
$ratelimiter = new RateLimiter(new ThrottlerFactory($cacheAdapter), new HydratorFactory(), $settings);
// 2. Get a throttler for path /login
$loginThrottler = $ratelimiter->get('/login');
// 3. Register a hit
$loginThrottler->hit()
// 4. Check if it reached the limit
if ($loginThrottler->check()) {
// access permitted
} else {
// access denied
}
// Or combine the steps 3 & 4
if ($loginThrottler->access()) {
// access permitted
} else {
// access denied
}
// To get the number of hits
print $loginThrottler->count(); // or count($throttler)
class DoctrineCacheAdapter implements CacheAdapterInterface
{
public function __construct($cache)
{
$this->cache = $cache;
}
// Implement the methods
}
// Build adapter using APC cache driver
$adapter = new DoctrineCacheAdapter(new \Doctrine\Common\Cache\ApcCache());
class RequestHydrator implements DataHydratorInterface
{
public function hydrate($data, $limit, $ttl)
{
// Make the key string
$key = $data->getClientIp() . $data->getPathInfo();
return new Data($key, $limit, $ttl);
}
}
// Hydrate the request to Data object
$hydrator = new RequestHydrator();
use Hydrator\FactoryInterface;
class MyHydratorFactory implements FactoryInterface
{
private $defaultFactory;
public function __construct(FactoryInterface $defaultFactory)
{
$this->defaultFactory = $defaultFactory;
}
public function make($data)
{
if ($data instanceof Request) {
return new RequestHydrator();
}
return $this->defaultFactory->make($data);
}
}
$cacheAdapter = new DesarrollaCacheAdapter((new DesarrollaCacheFactory())->make());
$timeAdapter = new PhpTimeAdapter();
$throttlerFactory = new TimeAwareThrottlerFactory($cacheAdapter, $timeAdapter);
$hydratorFactory = new HydratorFactory();
//$settings = ...
$ratelimiter = new RateLimiter($throttlerFactory, $hydratorFactory, $settings);
// Make a rate limiter with limit 120 attempts per minute
$settings = new FixedWindowSettings(120, 60);
// Make a rate limiter with limit 120 attempts per minute
$settings = new MovingWindowSettings(120, 60);
// Make a rate limiter with limit 120 attempts per minute, start delaying after 30 requests
$settings = new LeakyBucketSettings(120, 60000, 30);
// Make a leaky bucket ratelimiter which delays any overflow
$settings = new RetrialQueueSettings(new LeakyBucketSettings(120, 60000, 120));
json
"sunspikes/php-ratelimiter": "dev-master"
}
config/config.php
config.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.