https://github.com/akirk/php-ratelimiter
A small class that uses Memcache to allow only a certain number of requests per a certain amount of minutes.
https://github.com/akirk/php-ratelimiter
Last synced: 6 months ago
JSON representation
A small class that uses Memcache to allow only a certain number of requests per a certain amount of minutes.
- Host: GitHub
- URL: https://github.com/akirk/php-ratelimiter
- Owner: akirk
- License: other
- Created: 2013-04-19T07:35:24.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2018-04-26T12:43:31.000Z (over 7 years ago)
- Last Synced: 2025-04-30T16:13:02.376Z (9 months ago)
- Language: PHP
- Homepage: http://alexander.kirk.at/2013/04/19/add-a-rate-limit-to-your-website/
- Size: 125 KB
- Stars: 39
- Watchers: 4
- Forks: 14
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-php - php-ratelimiter
README
php-ratelimiter
===============
A small class that uses Memcache to allow only a certain number of requests per a certain amount of minutes.
The class works around the problem that the timeframe is constantly moving, i.e. every new minute the timeframe is different. See my [blogpost](http://alexander.kirk.at/2013/04/19/add-a-rate-limit-to-your-website/).
The code is released under an MIT license.
Usage
-----
```php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"]);
try {
// allow a maximum of 100 requests for the IP in 5 minutes
$rateLimiter->limitRequestsInMinutes(100, 5);
} catch (RateExceededException $e) {
header("HTTP/1.0 529 Too Many Requests");
exit;
}
```
Remarks
-------
The script creates a memcached entry per IP and minute.
If you want to protect multiple resources with different limits, use the third parameter of the constructor to namespace it:
```php
// script1.php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"], "script1");
try { ... }
// script2.php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"], "script2");
try { ... }
```
You can also use something else as a second parameter, for example a `session_id` to limit the requests per user instead of IP address.