Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Once in 10 seconds, 5 times per minute, 10 times per 5 minutes. */
- $rate_limits = array(10 => 1, 60 => 5, 300 => 10);
- $max_interval = max(array_keys($rate_limits)); /* 300 */
- /*
- For example:
- SELECT attempt_time FROM account_access_attempts
- WHERE account_id = ...
- AND attempt_time < NOW() - 300 seconds
- ORDER BY attempt_time DESC
- Or do a ZRANGE query to Redis, or whatever.
- Make sure access times are ordered, otherwise things will break.
- */
- # $attempt_times = get_account_access_times($username);
- $attempt_times = array(time() - 1, time() - 9, time() - 15);
- list($delay, $now, $count, $interval, $limit) = array(0, time(), 0, 0, 0);
- reset($rate_limits);
- foreach ($attempt_times as $ts) {
- $count++;
- if ($interval === 0 || $now - $ts > $interval) {
- list($interval, $limit) = each($rate_limits);
- if (!isset($interval)) { break; } # Safety measure
- }
- if ($count >= $limit) { $delay = max($delay, $interval - ($now - $ts)); }
- }
- unset($now, $count, $interval, $limit);
- echo $delay;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement