red-lolik/yii2-guest-rate-limiter

Rate limiter for guest users by IP address for Yii2 framework

Maintainers

Package info

github.com/Red-Lolik/yii2-guest-rate-limiter

Type:yii2-behavior

pkg:composer/red-lolik/yii2-guest-rate-limiter

Transparency log

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-06-12 15:19 UTC

This package is auto-updated.

Last update: 2026-07-12 16:01:10 UTC


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() or loadAllowance().
  • Pluggable storage – Uses Yii2's cache component (Redis, Memcached, File, etc.) by default.
  • Standard rate limit headers – Automatically returns:
    • X-Rate-Limit-Limit
    • X-Rate-Limit-Remaining
    • X-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

  1. Configure cache component.
    Make sure your application has a cache component configured (e.g., Redis, File).
    The package will use Yii::$app->cache automatically.
// config/web.php
'components' => [
    'cache' => [
        'class' => 'yii\redis\Cache',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ],
    ],
];
  1. 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.