askingtoomuch
askingtoomuch

Reputation: 537

Limiting Page Refresh on Laravel

I want to limit logged-in user from refreshing a certain page multiple-times (e.g. repeated pressing of F5 key). Is there any other simpler way other than counting the number of refresh and saving it on the database like this?

public function myPage()
{
    // Page is a table storing refresh count of pages
    $page = Page::where('user_id', Auth::user()->id)->get();
    $refresh_count = $page->refresh_count;
    
    if($refresh_count > 10){
       // logout user
    }else{
        $refresh_count = $page->refresh_count + 1;
        $page->update(['refresh_count' => $refresh_count]);
    }
    return view('mypage');
}

Upvotes: 2

Views: 621

Answers (1)

Bappi Saha
Bappi Saha

Reputation: 542

You can use Throttle middleware to limit access to a route or page refreshing.

The throttle middleware accepts two parameters that determine the maximum number of requests that can be made in a given number of minutes. For example, let's specify that an authenticated user may access the following group of routes 60 times per minute:

Route::middleware('auth', 'throttle:60,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});

For more details see documentation

Upvotes: 2

Related Questions