1.2.7 2019-04-15 13:23 UTC

This package is auto-updated.

Last update: 2024-09-16 01:23:00 UTC


README

PHP Throttle 是一个用于 Zend / Laravel 或 PHP 的速率限制器,由 Muhamed DidovicGoran Radosevic 制作和维护。

安装

PHP Throttle 需要 PHP 5.5 及以上版本

要获取最新版本,只需使用 Composer 引入项目

$ composer require muhamed-didovic/throttle

使用方法

$config = [
    'cache.path' => '/tmp',
    'cache.driver' => 'file',
    'limit' => 10,
    'time' => 1,
    'routes' => [
        [
            'url' => '/signup',
            'limit' => 10
        ],
        [
            'url' => '/signin',
            'limit' => 3,
        ],
        [
            'url' => '/signin',
            'limit' => 2,
            'method' => 'POST'
        ]
    ]
];

$throttle = (new ThrottleApp($config))->getThrottle();

if (!$throttle->attempt($request)) {
    echo "Rate limit exceeded. Please wait " . 60 . " sec."; die;
}

请注意,$request 在不同的系统中可能不同

ZEND

  • $request 是 Zend_Controller_Request_Http 的实例

LARAVEL

  • $request 是 Illuminate\Http\Request 的实例

数组

  • $request 是一个数组
$request = [
   'ip' => '127.0.0.1',
   'method' => 'POST',
   'route' => '/example-page'
];

对象

  • $request 是一个对象
$request = (object)[
   'ip' => '127.0.0.1',
   'method' => 'POST',
   'route' => '/example-page'
];

配置参数

所有配置参数都是可选的。您可以添加自己的参数来覆盖默认值。

cache.path

缓存文件夹的路径。默认为 "/tmp"

cache.driver

目前我们只支持 "file" 驱动类型

limit

在一段时间内允许的击中次数。默认为 100

time

检查尝试的时间段,以分钟为单位。默认为 1 分钟

routes

在此处添加要检查的特定路由。其他所有路由将不会用于节流。如果不提供此参数,则所有路由都将用于节流检查