PHPackages                             dxdo/rate-limit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. dxdo/rate-limit

ActiveLibrary[HTTP &amp; Networking](/categories/http)

dxdo/rate-limit
===============

Standalone component that facilitates rate-limiting functionality. Also provides a middleware designed for API and/or other application endpoints.

014PHP

Since Feb 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/dxdo/rate-limit)[ Packagist](https://packagist.org/packages/dxdo/rate-limit)[ RSS](/packages/dxdo-rate-limit/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Rate Limit
==========

[](#rate-limit)

[![Build Status](https://camo.githubusercontent.com/c0016482d4976900716b172bf18865b71ea4ac4bcd0c1b62ec01bd603a60fa63/68747470733a2f2f7472617669732d63692e6f72672f77656c6c696e6767757a6d616e2f726174652d6c696d69742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/wellingguzman/rate-limit)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/a503446e137768fcdb2051654538bf07174b2ea3c781b4bd09c31582524756be/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f77656c6c696e6767757a6d616e2f726174652d6c696d69742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/wellingguzman/rate-limit/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/64597898dfd82b4b850cacc23d39e87793eb356d460d9d2781401d5d4fe22d2a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f77656c6c696e6767757a6d616e2f77656c6c696e6767757a6d616e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/wellingguzman/rate-limit/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/4a8a0f9bba275053ff4b0ccd58ccef2567b5a1cecc1b91fec2f1d9a2d879e0d0/68747470733a2f2f706f7365722e707567782e6f72672f77656c6c696e6767757a6d616e2f726174652d6c696d69742f762f737461626c65)](https://packagist.org/packages/wellingguzman/rate-limit)

Component that facilitates rate-limiting functionality. Although designed as a standalone, it also provides a middleware designed for API and/or other application endpoints that be used with any framework that supports the middleware concept.

Based on `nikolaposa/rate-limit`. Type-hinting and `declare` function were removed to support PHP 5.6.

Installation
------------

[](#installation)

The preferred method of installation is via [Composer](http://getcomposer.org/). Run the following command to install the latest version of a package and add it to your project's `composer.json`:

```
composer require wellingguzman/rate-limit
```

Usage
-----

[](#usage)

### Standalone

[](#standalone)

```
$rateLimiter = \RateLimit\RateLimiterFactory::createInMemoryRateLimiter(1000, 3600);

echo $rateLimiter->getLimit(); //1000
echo $rateLimiter->getWindow(); //3600

$rateLimiter->hit('key');

echo $rateLimiter->getRemainingAttempts('key'); //999
echo $rateLimiter->getResetAt('key'); //1486503558
```

**Note**: in-memory rate limiter should only be used for testing purposes. This package also provides Redis-backed rate limiter:

```
$rateLimiter = \RateLimit\RateLimiterFactory::createRedisBackedRateLimiter([
    'host' => '10.0.0.7',
    'port' => 6379,
], 1000, 3600);
```

### Middleware

[](#middleware)

Zend Expressive example:

```
$app = \Zend\Expressive\AppFactory::create();

$app->pipe(\RateLimit\Middleware\RateLimitMiddleware::createDefault(
   \RateLimit\RateLimiterFactory::createRedisBackedRateLimiter([
       'host' => '10.0.0.7',
       'port' => 6379,
   ], 1000, 3600)
));
```

Slim example:

```
$app = new \Slim\App();

$app->add(\RateLimit\Middleware\RateLimitMiddleware::createDefault(
    \RateLimit\RateLimiterFactory::createRedisBackedRateLimiter([
       'host' => '10.0.0.7',
       'port' => 6379,
   ], 1000, 3600)
));
```

Whitelisting requests:

```
use Psr\Http\Message\RequestInterface;

$rateLimitMiddleware = \RateLimit\Middleware\RateLimitMiddleware::createDefault(
   \RateLimit\RateLimiterFactory::createRedisBackedRateLimiter([
        'host' => '10.0.0.7',
        'port' => 6379,
    ], 1000, 3600),
    [
        'whitelist' => function (RequestInterface $request) {
           if (false !== strpos($request->getUri()->getPath(), 'admin')) {
               return true;
           }

           return false;
        },
    ]
);
```

Custom limit exceeded handler:

```
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\Response\JsonResponse;

$rateLimitMiddleware = \RateLimit\Middleware\RateLimitMiddleware::createDefault(
    \RateLimit\RateLimiterFactory::createRedisBackedRateLimiter([
        'host' => '10.0.0.7',
        'port' => 6379,
    ], 1000, 3600),
    [
        'limitExceededHandler' => function (RequestInterface $request) {
           return new JsonResponse([
               'message' => 'API rate limit exceeded',
           ], 429);
        },
    ]
);
```

Author
------

[](#author)

**Nikola Poša**

-
-

Copyright and license
---------------------

[](#copyright-and-license)

Copyright 2017 Nikola Poša. Released under MIT License - see the `LICENSE` file for details.

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/e62a50dd8b389ce73c40a44c3817f6027936e6b22b04474e8243cfb947373a22?d=identicon)[dxdo](/maintainers/dxdo)

### Embed Badge

![Health badge](/badges/dxdo-rate-limit/health.svg)

```
[![Health](https://phpackages.com/badges/dxdo-rate-limit/health.svg)](https://phpackages.com/packages/dxdo-rate-limit)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
