atakde/php-rate-limiter

There is no license information available for the latest version (v1.0.0) of this package.

PHP Rate limiter

v1.0.0 2022-12-07 17:46 UTC

This package is not auto-updated.

Last update: 2024-05-25 12:37:54 UTC


README

A PHP package for rate limiting.

Installation

Install via composer

composer require atakde/php-rate-limiter

Usage

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$storage = new RedisStorage($redis);

// Allow maximum 10 requests in 60 seconds.
$rateLimitter = new RateLimiter([
    'refillPeriod' => 60,
    'maxCapacity' => 10,
    'prefix' => 'api'
], $storage);

$ip = $_SERVER['REMOTE_ADDR'];
if ($rateLimitter->check($ip)) {
    echo 'OK';
} else {
    echo 'Limit Exceeded';
}