PHP Throttle is a rate limiter for Zend / Laravel or PHP and was made and maintained by Muhamed Didovic and Goran Radosevic.
PHP Throttle requires PHP 5.5 and up
To get the latest version, simply require the project using Composer:
$ composer require muhamed-didovic/throttle
$config = [
'cache.path' => '/tmp',
'cache.driver' => 'file',
'limit' => 10,
'time' => 1,
'routes' => [
[
'url' => '/signup',
'limit' => 10
],
[
'url' => '/signin',
'limit' => 3,
],
[
'url' => '/signin',
'limit' => 2,
'method' => 'POST'
]
]
];
$throttle = (new ThrottleApp($config))->getThrottle();
if (!$throttle->attempt($request)) {
echo "Rate limit exceeded. Please wait " . 60 . " sec."; die;
}
Please note that $request can be different for different systems
- $request is an instance of Zend_Controller_Request_Http
- $request is na instance of Illuminate\Http\Request
- $request is an array
$request = [
'ip' => '127.0.0.1',
'method' => 'POST',
'route' => '/example-page'
];
- $request is an object
$request = (object)[
'ip' => '127.0.0.1',
'method' => 'POST',
'route' => '/example-page'
];
All configuration parameters are optional. Add your own to override the defaults.
Path to the cache folder. "/tmp" by default
Currently we support only "file" driver type
The number of allowed hits in a period of time. Default: 100
A period of time, in minutes where we check for attempts. Default 1, minute.
Add here specific routes to check. All other routes will not be used against throttle. If you don't provide this parameter, all routes will be checked against throttle