StackPHP Middleware based on Rack::Attack
PHP
Latest commit 6c9f2fa Nov 1, 2013 @dongilbert dongilbert add link to stackphp

README.md

League\StackAttack

StackAttack is a blocking & throttling middleware for StackPHP, based heavily off of Rack::Attack for Ruby. It currently allows whitelisting and blacklisting.

Install Via Composer

{
    "require": {
        "league/stack-attack": "~1.0@dev"
    }
}

Example

include_once '../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use League\StackAttack\FilterCollection;

$app = new Stack\CallableHttpKernel(function (Request $request) {
    return new Response('Hello World!');
});

$filters = (new FilterCollection)
    ->blacklist('Block dev requests.', function (Request $request) {
        return strpos($request->getPathInfo(), '/dev') === 0;
    });

$config = [
    'blacklistedResponse' => function (Request $request) {
            // A 503 response makes some bots think they had a successful DDOS
            return new Response('Service Unavailable', 503);
        }
];

$app = (new Stack\Builder)
    ->push('League\\StackAttack\\Attack', $filters, $config)
    ->resolve($app);

Stack\run($app);