Download the PHP package anjola/php_rate_limiter without Composer
On this page you can find all versions of the php package anjola/php_rate_limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anjola/php_rate_limiter
More information about anjola/php_rate_limiter
Files in anjola/php_rate_limiter
Package php_rate_limiter
Short Description A PHP Library that seamlessly integrate limiting into your platform, supports Redis ,FileSystem , Datbases such as SQL Relational Database.
License Apache-2.0
Informations about the package php_rate_limiter
PHP Rate Limiter
A PHP Library that seamlessly integrates rate limiting into your platform. It supports multiple storage backends including Redis, File System, and SQL Relational Databases, providing flexible and efficient request limiting capabilities.
Table of Contents
- Introduction
- Installation
- Configuration
- Usage
- RateLimiter
- Analytics
- Storage Options
- Examples
- License
- Author
Introduction
The PHP Rate Limiter library provides a simple and extensible way to limit the number of requests or actions a user or system can perform within a specified time window. It supports multiple storage backends to suit different environments and scales, including Redis, file-based storage, SQL databases, and session storage.
Additionally, the library includes an Analytics component to track custom events, allowing you to monitor usage patterns or other metrics alongside rate limiting.
Installation
You can install the library via Composer:
Requirements
- PHP 7.2.5 or higher
- Extensions:
fileinfo
- Dependencies:
vlucas/phpdotenv
for environment variable managementpredis/predis
for Redis support
Configuration
The library uses environment variables to configure storage options. You can create a .env
file in your project root to set these variables.
Important Environment Variables
Variable | Description | Default |
---|---|---|
STORAGE_DRIVER |
Storage backend to use (redis , file , database , session ) |
sqlite (file-based) |
ANALYTICS_STORAGE_FILE_NAME |
File name for analytics storage when using file driver | analytics_storage.json |
RATELIMITER_STORAGE_FILE_NAME |
File name for rate limiter storage when using file driver | ratelimiter_storage.json |
Make sure to configure your environment according to your preferred storage backend.
Usage
RateLimiter
The RateLimiter
class allows you to limit the number of attempts for a given key within a decay period.
Initialization
Check if too many attempts
Clear records for a key
Analytics
The Analytics
class allows you to track custom events and retrieve their counts.
Initialization
Track an event
Get event count
Reset event count
Storage Options
The library supports multiple storage backends to store rate limiting and analytics data. You can configure the storage driver via the STORAGE_DRIVER
environment variable.
Supported Drivers
-
Redis (default)
- Requires Redis server and
predis/predis
package. - High performance and suitable for distributed environments.
- Requires Redis server and
-
File System
- Stores data in JSON files.
- Configurable file names via environment variables.
- Suitable for simple or local setups.
-
Database
- Supports SQL relational databases.
- Requires appropriate database setup and configuration.
- Session
- Uses PHP session storage.
- Suitable for per-session rate limiting.
Configuration Example
Set the storage driver in your .env
file:
Or for file storage:
Examples
Basic Rate Limiting Example
Analytics Tracking Example
License
This project is licensed under the Apache-2.0 License. See the LICENSE file for details.
Author
Anjolaanuoluwapo
Email: [email protected]
Thank you for using PHP Rate Limiter! If you have any questions or issues, feel free to open an issue or contact the author.