red-lolik / yii2-guest-rate-limiter
Rate limiter for guest users by IP address for Yii2 framework
Package info
github.com/Red-Lolik/yii2-guest-rate-limiter
Type:yii2-behavior
pkg:composer/red-lolik/yii2-guest-rate-limiter
Requires
- php: >=8.0
- yiisoft/yii2: ~2.0.0
README
Forked, simplified and actualized from andrey-tm/yii2-ip-ratelimiter.
π Introduction
Simple and powerful rate limiting for guest (unauthenticated) users in Yii2.
By default, Yii2's built-in RateLimiter filter heavily relies on the User component and the RateLimitInterface.
This makes it unnecessarily complex β and often broken β when you need to rate-limit guests based on IP address
or custom fingerprint.
This package solves that problem. It allows you to apply rate limits to guest requests without modifying your User model, without forcing authentication, and with minimal configuration.
π― Features
- Guestβfirst design β Works out of the box for unauthenticated users.
- IPβbased rate limiting β Uses real client IP (supports
X-Forwarded-For). - No User model changes β No need to implement
getRateLimit()orloadAllowance(). - Pluggable storage β Uses Yii2's
cachecomponent (Redis, Memcached, File, etc.) by default. - Standard rate limit headers β Automatically returns:
X-Rate-Limit-LimitX-Rate-Limit-RemainingX-Rate-Limit-Reset
- Customizable key β Use a custom string to generate your own fingerprint (e.g., IP + User-Agent).
- PSR-12 friendly β Works with Yii2 REST and web controllers.
π¦ Installation
composer require red-lolik/yii2-guest-rate-limiter
β‘ Quick Start
- Configure cache component.
Make sure your application has a cache component configured (e.g., Redis, File).
The package will useYii::$app->cacheautomatically.
// config/web.php 'components' => [ 'cache' => [ 'class' => 'yii\redis\Cache', 'redis' => [ 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], ], ];
- Attach behavior to any controller
use RedLolik\yii2GuestRateLimiter\GuestRateLimiter; class ApiController extends \yii\rest\Controller { public function behaviors() { $behaviors = parent::behaviors(); $behaviors['guestRateLimiter'] = [ 'class' => GuestRateLimiter::class, 'rateLimit' => 5, 'timePeriod' => 600, 'actions' => ['sign-in'], ]; return $behaviors; } }
That's it! Guests (unauthenticated users) will now be rate-limited by their IP address.
π§ Advanced Configuration
'guestRateLimiter' => [ 'class' => GuestRateLimiter::class, 'cache' => 'redisCache', // Custom cache component. 'separateUsers' => true, // β οΈ Your UserIdentity model should implement RateLimitInterface. 'cacheKey' => Yii::$app->request->queryString, // Custom cache key. 'actions' => ['sign-in'], // Array of action names to which the behavior applies. Applies to all actions if empty. 'rateLimit' => 5, // Maximum number of queries per time period. 'timePeriod' => 600, // Time period in seconds. ]
π§ͺ Real-World Use Cases Identified From Developer Questions
| Problem from forums / Stack Overflow | This package solution |
|---|---|
| "RateLimiter only works for logged-in users" | Works without Yii::$app->user->id |
| "I get 401 errors when trying to limit guests" | No authentication required |
| "My User model doesn't implement getRateLimit()" | No changes to User model |
| "Rate limiting resets across multiple servers" | Uses central cache (Redis ready) |
| "How to limit by IP for contact form spam?" | Ready with 3 lines of config |
π Requirements
- PHP 8.0 or later
- Yii2 >= 2.0.0
π€ Contributing
Issues and pull requests are welcome. Please report any bugs or feature requests via GitHub Issues.
π License
This package is open-sourced software licensed under the MIT license.