Download the PHP package aporat/laravel-rate-limiter without Composer

On this page you can find all versions of the php package aporat/laravel-rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-rate-limiter

Laravel Rate Limiter

Latest Stable Version Downloads Codecov GitHub Actions Workflow Status License

Redis-backed, multi-window rate limiting for Laravel: a route middleware with per-IP, per-user and per-route keys, plus a fluent API for limiting anything else.

Features

Requirements

Installation

The service provider is auto-discovered. The facade is Aporat\RateLimiter\Facades\RateLimiter. It isn't aliased globally, because Laravel already has a RateLimiter facade.

Configuration

config/rate-limiter.php:

Key Default Description
connection default (RATE_LIMITER_REDIS_CONNECTION) A connection from config/database.php → redis.
prefix rate-limiter Namespace for the package's keys, added after the connection's own options.prefix.
limits hourly=3000, minute=60, second=10 Middleware windows when a route passes none. Windows: second, minute, hourly, daily. 0 disables a window.
key ip What the middleware counts by when a route passes no key.
whitelisted_ips 127.0.0.1/32, ::1/128 Addresses or CIDR ranges that skip the middleware.
headers true Add X-RateLimit-* headers to successful responses (429 responses always get them).
log_errors false Log one warning per 429.
block_seconds 86400 Default length of blockIpAddress().
on_redis_failure open open or closed; see When Redis is down.
failure_backoff_seconds 5 How long to stop calling Redis after a failure.

Give the limiter its own Redis connection with short timeouts so a slow Redis can't stall requests:

Behind a load balancer or proxy

The middleware keys on $request->getClientIp(), so it depends on Laravel's trusted proxy setup:

The middleware

The provider registers the rate.limiter alias.

Parameter Meaning
N,M (positional) N requests per M minutes (M defaults to 1), like Laravel's throttle. Can be followed by named options.
second=, minute=, hourly=/hour=, daily=/day= Limit per window. When any window is given, only the given windows apply. 0 disables a window.
key= ip, user or route, combined with + (e.g. user+route). user is $request->user()->getAuthIdentifier(), falling back to the IP for guests. route is the route name, or its URI pattern (users/{user}), so the number of keys stays bounded. Default: config key.
name= Gives this group its own counters. Without a name, every unnamed rate.limiter route with the same key shares one budget per window.

Unknown or malformed parameters throw an InvalidArgumentException on the first request, so typos don't silently fall back to the defaults.

You can also build the string in PHP:

Order of operations for each request:

  1. A whitelisted IP passes straight through.
  2. A blocked IP gets a 429 with Retry-After set to the time left on the block.
  3. Every window is checked, then the request is counted in all of them, or in none if any window is full.

A request without a client IP, and with nothing else to key on (for example key=ip with no IP), is let through unlimited instead of sharing one bucket with every other such request. A real web server always provides one.

To register it globally:

It is independent of Laravel's own throttle middleware and RateLimiter facade: different storage and different keys. Use one or the other on a route, not both.

Headers

Successful responses (when headers is on):

These describe the most restrictive window: the one with the fewest requests left, with ties going to the window that resets last.

A refused request is a 429 Too Many Requests with the same headers plus Retry-After, describing the full window that resets last. Every full window must reset before a retry can succeed.

Fixed windows and bursts

Each window is a fixed window that starts at its first hit and lasts its length. The usual fixed-window caveat applies: a client can spend a full window's budget just before the window resets and another full budget just after it. With second=10, up to 20 requests can land within a fraction of a second across the boundary. Pair a short window with a longer one (for example second=10,minute=300) if that matters.

Limiting anything else

RateLimiter::create() returns an immutable PendingLimit. Every with*() call returns a new instance, so nothing leaks between calls.

Method Notes
withClientIpAddress() Needs a request with a client IP. IPv6 is grouped by /64.
withUserId($id), withName($name) Free-form; segments are escaped, so 'a:b' can't collide with 'a' + 'b'.
withRequestInfo() Method plus route name or URI pattern (the raw path when no route is matched).
withTimeInterval($seconds) Window length, default 3600.
limit($max, $amount = 1) Returns a LimitResult, or throws RateLimitException. A refused attempt isn't counted.
attempt($max, $amount = 1) Same as limit(), without throwing.
record($amount = 1) Always counts, and returns the new count.
count(), ttl(), clear() Inspect or reset the counter.

$max and $amount must be at least 1.

Blocking IPs

The middleware answers blocked IPs with a 429 and Retry-After. IPv6 blocks cover the whole /64.

When Redis is down

on_redis_failure Behaviour
open (default) Requests go through unchecked (no rate limit headers). attempt()/limit() return an allowed result with degraded = true; record(), count() and ttl() return 0.
closed Requests get 503 Service Unavailable with Retry-After (RateLimiterUnavailableException).

After a failure, the process doesn't contact Redis again for failure_backoff_seconds, so a dead or unreachable Redis costs one timeout per backoff period, not one per request. The Redis exception is passed to report() once per backoff period. The backoff is per PHP process: per FPM worker, or per Octane worker.

Admin operations (blockIpAddress, unblockIpAddress, clear, flushAll) always throw on a Redis error.

Events

Logging

RateLimitException::report() stops Laravel from logging every 429 as an error with a stack trace. With log_errors on, it writes a single warning, Rate limit exceeded, with the method, path, client IP and retry-after. It never logs the query string, input or headers. To react to refusals, listen for RateLimitExceeded.

Octane and long-running workers

The RateLimiter singleton holds no request state. It resolves config and the Redis connection from the current container on every call, so runtime config() changes are picked up. Builders are immutable objects created per call. The only process-wide state is the Redis failure backoff, which is intended.

Redis Cluster

All windows of one subject share a {...} hash tag, so the multi-window script touches a single slot. flushAll() scans one node and throws on cluster connections.

Upgrading from v5

v6 is a breaking release.

Storage and config

Middleware

Builder API

Exceptions

Requirements

Testing

The suite needs a Redis server (database 15; REDIS_HOST/REDIS_PORT override the address) and runs against either client:

License

MIT. See LICENSE.


All versions of laravel-rate-limiter with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/contracts Version ^12.0 || ^13.0
illuminate/http Version ^12.0 || ^13.0
illuminate/log Version ^12.0 || ^13.0
illuminate/redis Version ^12.0 || ^13.0
illuminate/routing Version ^12.0 || ^13.0
illuminate/support Version ^12.0 || ^13.0
symfony/http-foundation Version ^7.2 || ^8.0
symfony/http-kernel Version ^7.2 || ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package aporat/laravel-rate-limiter contains the following files

Loading the files please wait ...