Download the PHP package los/los-rate-limit without Composer
On this page you can find all versions of the php package los/los-rate-limit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download los/los-rate-limit
More information about los/los-rate-limit
Files in los/los-rate-limit
Package los-rate-limit
Short Description Rate Limit Middleware for PHP
License BSD-3-Clause
Homepage https://github.com/LansoWeb/LosRateLimit
Informations about the package los-rate-limit
Rate Limit Middleware for PHP
LosRateLimit is a php middleware to implement a rate limit.
First, the middleware will look for an X-Api-Key header to use as key. If not found, it will fall back to the remote IP.
Each one has its own limits (see configuration below).
Attention! This middleware does not validate the Api Key, you must add a middleware before this one to validate it.
Requirements
- PHP >= 8.0
- Psr\SimpleCache implementation
Installation
Configuration
max_requests
How many requests are allowed before the reset time (using API Key)reset_time
After how many seconds the counter will be reset (using API Key)ip_max_requests
How many requests are allowed before the reset time (using remote IP Key)ip_reset_time
After how many seconds the counter will be reset (using remote IP Key)api_header
Header name to get the api key from.trust_forwarded
If the X-Forwarded (and similar) headers and be trusted. If not, only $_SERVER['REMOTE_ADDR'] will be used.prefer_forwarded
Whether forwarded headers should be used in preference to the remote address, e.g. if all requests are forwarded through a routing component or reverse proxy which adds these headers predictably. This is a bad idea unless your app can only be reached this way.forwarded_headers_allowed
An array of strings which are headers you trust to contain source IP addresses.forwarded_ip_index
If null (default), the first plausible IP in an XFF header (reading left to right) is used. If numeric, only a specific index of IP is used. Use-2
to get the penultimate IP from the list, which could make sense if the header always ends...<client_ip>, <router_ip>
. Or use0
to use only the first IP (stopping if it's not valid). Likeprefer_forwarded
, this only makes sense if your app's always reached through a predictable hop that controls the header - remember these are easily spoofed on the initial request.keys
Specify different max_requests/reset_time per api keyips
Specify different max_requests/reset_time per IPhash_ips
Enable the hashing of IP addresses before storing them. This is particularly useful when using a filesystem-based cache implementation and working with IPv6 addresses. A salted MD5-hash will be used if you set this totrue
.hash_salt' This setting allows you to optionally define a custom salt when using hashed IP addresses. Only effective when
hash_ipsis
true`.
The values above indicate that the user can trigger 100 requests per hour.
If you want to disable ip access (e.g. allowing just access via X-Api-Key), just set ip_max_requests to 0 (zero).
Usage
Just add the middleware as one of the first middlewares.
The provided factory uses the container to get a \Psr\SimpleCache\CacheInterface (PSR-16). Most implementations provide both PSR-6 and PSR-16, or at least a decorator. Recommended: zend-cache or symfony/cache.
Laminas / Mezzio
If you are using mezzio-skeleton,
you can copy config/los-rate-limit.local.php.dist
to
config/autoload/los-rate-limit.local.php
and modify configuration as your needs.
All versions of los-rate-limit with dependencies
psr/http-message Version ^1.0
psr/container Version ^2.0
psr/http-server-middleware Version ^1.0
mezzio/mezzio-problem-details Version ^1.0
laminas/laminas-diactoros Version ^2.0
psr/simple-cache Version ^1.0 || ^2.0 || ^3.0