Skip to content
PHP Rate Limiting Library With Token Bucket Algorithm
PHP
Branch: master
Clone or download
Pull request Compare This branch is 14 commits ahead, 1 commit behind touhonoob:master.
Latest commit 9270be7 Oct 7, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src docs Oct 7, 2019
tests fix formatting Aug 18, 2019
.codeclimate.yml added .codeclimate.yml May 16, 2015
.gitignore remove ext-apc from required extension Jun 6, 2016
.php_cs.dist add php-cs-fixer Feb 18, 2018
.travis.yml drop this - duplicated Sep 18, 2019
README.md update docs: fix predis connection settings - see touhonoob#19 Aug 18, 2019
composer.json bump stash version Sep 18, 2019
psalm.xml try silencing psalm Dec 29, 2018

README.md

RateLimit

Build Status

PHP Rate Limiting Library With Token Bucket Algorithm with minimal external dependencies.

Installation

composer require palepurple/rate-limit

Storage Adapters

The RateLimiter needs to know where to get/set data.

Depending on which adapter you install, you may need to install additional libraries (predis/predis or tedivm/stash) or PHP extensions (e.g. Redis, Memcache, APC)

Example

require 'vendor/autoload.php';

use \PalePurple\RateLimit\RateLimit;
use \PalePurple\RateLimit\Adapter\APC as APCAdapter;
use \PalePurple\RateLimit\Adapter\Redis as RedisAdapter;
use \PalePurple\RateLimit\Adapter\Predis as PredisAdapter;
use \PalePurple\RateLimit\Adapter\Memcached as MemcachedAdapter;
use \PalePurple\RateLimit\Adapter\Stash as StashAdapter;


$adapter = new APCAdapter(); // Use APC as Storage
// Alternatives:
//
// $adapter = new RedisAdapter((new \Redis()->connect('localhost'))); // Use Redis as Storage
//
// $adapter = new PredisAdapter(new \Predis\Predis(['tcp://127.0.0.1:6379'])); // Use Predis as Storage
//
// $memcache = new \Memcached();
// $memcache->addServer('localhost', 11211);
// $adapter = new MemcacheAdapter($memcache); 
//
// $stash = new \Stash\Pool(new \Stash\Driver\FileSystem());
// $adapter = new StashAdapter($stash);

$rateLimit = new RateLimit("myratelimit", 100, 3600, $adapter); // 100 Requests / Hour

$id = $_SERVER['REMOTE_ADDR']; // Use client IP as identity
if ($rateLimit->check($id)) {
  echo "passed";
} else {
  echo "rate limit exceeded";
}

Installing via Composer

curl -sS https://getcomposer.org/installer | php
composer.phar require palepurple/rate-limit

References

You can’t perform that action at this time.