Block user for half hour if he exceeded Throttling limits in one minute ( Laravel 5.6)

Viewed 1773

I'm using Throttling and I notice you can allow number requests per specific amount of time but I'm trying to do something differnt

if I set Throttling 60:1

so it's allowed to send 60 request every one minute

But is it possible to make it like this Throttling 60:1:30

like if the user made 60 request in one minute then block him for 30 minutes

1 Answers

Customizing the throttle middleware

If we want to limit it to 5 attempts per minute.

Route::group(['prefix' => 'api', 'middleware' => 'throttle:5'], function () {
    Route::get('people', function () {
        return Person::all();
    });
});

And if we want to change it so that, if someone hits the limit, they can't try again for another 10 minutes.

Route::group(['prefix' => 'api', 'middleware' => 'throttle:5,10'], function ()     {
    Route::get('people', function () {
        return Person::all();
    });
});

Update

you need to add a date time flag in db banned_at and set it when the rate limit is reached. set that date time by adding 30 min to current date time, and compare it when when user accesses route with the help of middleware. when the user accesses within 30 min of rate limit reaching, the date time in db banned_at will be greater than current time, and you can block him in that case.

Update

there is another way, see if you can set $key variable in RateLimiter class here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Cache/RateLimiter.php#L38 , i could not find way to set it

Hope this helps you.

Related