PHP code example of michaelesmith / throttle
1. Go to this page and download the library: Download michaelesmith/throttle 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/ */
michaelesmith / throttle example snippets
$throttle = new Throttle(new \Cache\Adapter\PHPArray\ArrayCachePool());
$throttle->add(new Condition(60, 2)); // adds an interval where 2 increments are allowed in 60 seconds
$throttle->add(new Condition(600, 5)); // adds an interval where 5 increments are allowed in 10 minutes
// in each action you want to limit
try {
$throttle->increment($_SERVER['REMOTE_ADDR']); // some client identifier like an ip or session id
// NOTE: $_SERVER['REMOTE_ADDR'] may not gove you the actual client IP if you are behind a reverse proxy
// Here is how Symfony finds the client IP
// @link: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L786-L805
} catch(RateException $e) {
$condition = $e->getCondition(); // the condition that hit the rate limit
printf('You can only make %d requests in %d seconds', $condition->getLimit(), $condition->getTtl());
}