PHP code example of bm2ilabs / rate-limiter

1. Go to this page and download the library: Download bm2ilabs/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/ */

    

bm2ilabs / rate-limiter example snippets


// In routes/api.php
Route::middleware('multi-rate-limit')->group(function () {
    // Your API routes here
});

Route::get('/endpoint', 'YourController@method')->middleware('multi-rate-limit');

use Amineware\RateLimiter\Facades\MultiPeriodRateLimiter;

// Check remaining attempts
$remaining = MultiPeriodRateLimiter::remaining('minute', $userId);

// Check if too many attempts
if (MultiPeriodRateLimiter::tooManyAttempts('hour', $userId, 100)) {
    // Handle rate limit exceeded
}

// Increment the counter
MultiPeriodRateLimiter::hit('day', $userId);

// Clear rate limits for a key
MultiPeriodRateLimiter::clear('minute', $userId);

// In config/rate-limiter.php
'identifiers' => [
    'default' => 'custom',
    'custom_parameter' => 'api_key',
],
bash
php artisan vendor:publish --provider="Amineware\RateLimiter\RateLimiterServiceProvider"