perimeter / rate-limiter-php
A very simple Rate Limiter for PHP
Installs: 14 702
Dependents: 1
Suggesters: 0
Security: 0
Stars: 17
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.0
- predis/predis: 1.0.*
Requires (Dev)
- doctrine/orm: 2.4.*
Suggests
- perimeter/cache-bundle: Required to use the Cache storage engine
- perimeter/rate-limit-bundle: allows you to integrate with symfony2
This package is not auto-updated.
Last update: 2024-12-21 17:58:42 UTC
README
Rate Limit those APIs!
Installation
$ composer.phar require perimeter/rate-limiter-php:dev-develop
This library can be used alongside the perimeter/RateLimitBundle for Symfony2 applications. See the repository for more instructions.
Get Started
Create your throttler:
include_once('vendor/autoload.php'); $redis = new Predis\Client(); $throttler = new Perimeter\RateLimiter\Throttler\RedisThrottler($redis);
Ensure redis is running by executing the redis-server
command on the web server where
the library is running:
$ redis-server
Now you can throttle your users accordingly!
// Create a meter ID based on something unique to your user // In this case we use the IP. This can also be a username, // company, or some other authenticated property $meterId = sprintf('ip_address:%s', $_SERVER['REMOTE_ADDR']); $warnThreshold = 10; $limitThreshold = 20; // run the "consume" command $throttler->consume($meterId, $warnThreshold, $limitThreshold); if ($throttler->isLimitWarning()) { echo "slow down!"; } if ($throttler->isLimitExceeded()) { exit("you have been rate limited"); }
And that's it!