PHP code example of aporat / laravel-rate-limiter

1. Go to this page and download the library: Download aporat/laravel-rate-limiter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aporat / laravel-rate-limiter example snippets


return [
    // ...
    Aporat\RateLimiter\RateLimiterServiceProvider::class,
];

use Aporat\RateLimiter\Facades\RateLimiter;

return [
    'limits' => [
        'hourly' => 3000,
        'minute' => 60,
        'second' => 10,
    ],

    // Exempt from the middleware. Plain addresses or CIDR ranges, IPv4 or IPv6.
    'whitelisted_ips' => ['127.0.0.1', '::1', '10.0.0.0/8'],

    // Add X-Rate-Limit-Limit / X-Rate-Limit-Remaining to middleware responses.
    'headers' => false,

    // When false, RateLimitException is handed back to your application's own
    // reporters instead of writing its own log line.
    'log_errors' => true,

    'block_seconds' => 86400,

    'redis' => [
        // Inherit host/port/credentials from a config/database.php connection.
        'connection' => env('RATE_LIMITER_REDIS_CONNECTION'),
        'host' => env('RATE_LIMITER_REDIS_HOST', '127.0.0.1'),
        'port' => env('RATE_LIMITER_REDIS_PORT', 6379),
        'username' => env('RATE_LIMITER_REDIS_USERNAME'),
        'password' => env('RATE_LIMITER_REDIS_PASSWORD'),
        'database' => env('RATE_LIMITER_REDIS_DB', 0),
        'prefix' => env('RATE_LIMITER_REDIS_PREFIX', 'rate-limiter'),
        'timeout' => env('RATE_LIMITER_REDIS_TIMEOUT', 2.0),
        'read_timeout' => env('RATE_LIMITER_REDIS_READ_TIMEOUT', 2.0),
    ],
];

Route::middleware('rate.limiter')->group(function () {
    // ...
});

->withMiddleware(function (Middleware $middleware) {
    $middleware->append(\Aporat\RateLimiter\Middleware\RateLimit::class);
})

use Aporat\RateLimiter\Facades\RateLimiter;

Route::post('/submit', function (Request $request) {
    RateLimiter::create($request)
        ->withUserId((string) $request->user()?->id ?: 'guest')
        ->withName('form_submission')
        ->withTimeInterval(3600)
        ->limit(5); // 5 submissions per hour

    return 'Submitted!';
});

$count = RateLimiter::create($request)
    ->withRequestInfo()
    ->withUserId($userId)
    ->withTimeInterval(10)
    ->record();

if ($count > 1) {
    // duplicate within the window
}

RateLimiter::blockIpAddress('192.168.1.1', 86400); // defaults to block_seconds
RateLimiter::unblockIpAddress('192.168.1.1');

if (RateLimiter::create($request)->isIpAddressBlocked()) {
    abort(403, 'Your IP is blocked.');
}

$response = new Response('OK');

RateLimiter::create($request)
    ->withResponse($response)
    ->withRateLimitHeaders()
    ->withTimeInterval(3600)
    ->limit(100);

return $response; // X-Rate-Limit-Limit and X-Rate-Limit-Remaining

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->report(function (RateLimitException $e) {
        // runs when rate-limiter.log_errors is false
    });
})
bash
php artisan vendor:publish --provider="Aporat\RateLimiter\RateLimiterServiceProvider" --tag="config"