Download the PHP package mata-sh/rate-limiter without Composer

On this page you can find all versions of the php package mata-sh/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 rate-limiter

Rate Limiter

Atomic sliding-window rate limiting for PHP applications. Supports filesystem, Redis, and APCu storage.

Installation

Requires PHP 8.0 or later.

Basic usage

Pass a storage implementation explicitly. consume() atomically checks a limit and consumes one request when it allows it.

RateLimitResult provides isAllowed(): bool, getCurrent(): int, and getRetryAt(): ?int. retryAt is null for allowed requests. For denied requests, it is calculated atomically as the oldest request still in the sliding window plus the window duration.

allow() remains a compatibility convenience wrapper around consume() when only a boolean is needed:

check() reads the current state without consuming a request:

check() returns:

Field Meaning
allowed Whether current is below limit.
current Requests currently counted in the sliding window.
limit The effective maximum request count.
remaining Requests still allowed, never below zero.
storage_type The configured storage's getName() value (built-ins return file, apcu, or redis).

check() is non-atomic and does not provide retry guidance. When rate limiting is disabled, it returns allowed: true, current: 0, limit and remaining as PHP_INT_MAX, and storage_type: 'disabled'.

Reset and cleanup

Reset one identifier through the limiter facade. It uses the same storage key translation as consume(), allow(), and check():

There is no global reset or storage scan API. Reset each identifier explicitly.

Use cleanup() to remove expired records from storage backends that require maintenance:

cleanup() requires a limit greater than zero. Every storage implements the same operation. APCu and Redis manage expiration with TTLs, so they return 0; filesystem storage actively removes expired files.

Configuration and logging

Rate limiting starts enabled with defaults of 60 requests per 60 seconds. Set defaults for calls that omit the optional limit arguments:

Both configuration values must be greater than zero. Inspect the active settings or temporarily bypass limiting:

The constructor accepts an optional PSR-3 logger:

Without a logger, the library uses Psr\Log\NullLogger.

Storage backends

All storage implementations implement MataSh\RateLimiter\Storage\StorageInterface, so custom backends can be injected into RateLimiter.

FileStorage

The directory must be writable and traversable. If FileStorage creates it, it uses mode 0700. Permissions on an existing directory are caller-managed and remain unchanged. FileStorage uses SHA-256-derived .json state filenames and locks state updates. If no directory is supplied, it uses sys_get_temp_dir() . '/cache/rate_limit'.

Expired files are not removed automatically. Schedule bounded cleanup through the limiter facade as appropriate for the application:

FileStorage::cleanup() performs this maintenance directly.

ApcuStorage

Install the APCu PHP extension and ensure APCu is enabled. APCu state is local to the PHP environment that shares its APCu cache; it is not a distributed backend. Operations use an APCu lock to make consumption atomic.

RedisStorage

Requires the PHP redis extension and a connected Redis instance. Redis consumption uses optimistic transactions and retries conflicts up to five times.

Storage failures

Backend initialization and storage operations can throw MataSh\RateLimiter\Storage\StorageException, including unavailable APCu, filesystem errors, Redis errors, malformed state, and lock or transaction failures. Handle this exception according to the application's fail-open or fail-closed policy.

Identifiers

The identifier factory methods return prefixed SHA-256-based identifiers. fromIp() and getClientIp() use $_SERVER['REMOTE_ADDR'] only; they do not inspect or trust proxy headers. When the application is behind a proxy, pass the verified client IP explicitly to fromIp($ip) or fromIpUser($username, $ip).

License

MIT


All versions of rate-limiter with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
psr/log Version ^3.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 mata-sh/rate-limiter contains the following files

Loading the files please wait ...