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/ */
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
});
})