Download the PHP package gnikolovski/cb-rate-limiter without Composer
On this page you can find all versions of the php package gnikolovski/cb-rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gnikolovski/cb-rate-limiter
More information about gnikolovski/cb-rate-limiter
Files in gnikolovski/cb-rate-limiter
Package cb-rate-limiter
Short Description API rate limiter which uses Couchbase for storing data
License MIT
Homepage https://github.com/gnikolovski/cb-rate-limiter
Informations about the package cb-rate-limiter
CB Rate Limiter
Couchbase API Rate Limiter is a super simple PHP package for limiting access to your public API. It was originally created for one of my projects, and I'm using it with Slimframework, but it could be used in any of your projects.
I decided to go with Couchbase, because the rest of my application was using this database to store data. Couchbase is a super fast NoSQL database, so it is perfect for this kind of tasks.
How to install?
The easiest and recommended method to install CB Rate Limiter is via composer:
How to use it?
Best way to use this package is as a middleware on your routes. Put it first - before any other middleware, and if user has reached the limit you imposed, you should return http code 429 (too many requests) with appropriate headers.
This is how you could use this package with Slimframework as a middleware:
Class is instantiated with the following three variables in the constructor:
$hostname - IP address of your Couchbase server
$bucket - name of the bucket where you will store data
$password - database password
After you create $limiter object you have to call isExceeded() method and provide the following data:
$ip_address - IP address of user trying to access API route
$max_requests - max requests you are willing to accept in a unit of time
$in_minutes - unit of time
IP address of the user could be supplied using global PHP variable:
or by using some package, like akrabat/rka-ip-address-middleware.
Variables $max_requests and $in_minutes should be integers. If you want to accept 100 requests from one IP address in 60 minutes you would write:
Requirements
To use Couchbase in PHP you must install PHP SDK. To find out more visit: (http://developer.couchbase.com/documentation/server/4.0/sdks/php-2.0/download-links.html)
AUTHOR
Goran Nikolovski - https://gorannikolovski.com