Actually, here is my flooding control function.

public function StopFlud($user) { $search_user =\DB::table('stop_flud') ->where('User_id', $user)->first(); // Ищем пользователя в базе if(empty($search_user)) // Если пользователя нет в базе, то создаём строку с временем его последней ставки { \DB::table('stop_flud')->insertGetId(['User_id' => $user, 'last_date' => time()]); } else { // Иначе просто подгружаем ему время последней ставки if((time()-$search_user->last_date) < 2) // Если прошло меньше 2 секунд c момента его последней ставки, то FALSE { return 'false'; } else { \DB::table('stop_flud') ->where('User_id', $user) ->update(['last_date' => time()]); return 'true'; } } } 

As a result, the function does not work. What am I doing wrong? two hours already sitting over her

  • why do you need a separate table if you already have a users table. Why it is impossible to create a separate column there? - n.osennij
  • @ n.osennij because I need it that way. - Roman Kashirov
  • and not to use all the advantages of Eloquent and not to fence, God knows what - this is also so necessary? - n.osennij
  • Do you want the user to post a comment no more than once in a while? - n.osennij
  • What is your version of the framework? - n.osennij

1 answer 1

To limit, use the built-in middleware - trottle . Thus, it is possible to limit the number of requests at a given time to a certain route. At the same time, the restriction works for each individual user.

Example from documentation

For example, let's say the following group of routes is 60 times per minute:

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

Two middlewares are applied to the route, one of which is the number of requests per minute. In this case, no more than 60 per minute.

In your case, something like this (if a group of routes)

 Route::middleware('throttle:1,0.03')->group(function () { Route::post('/action', 'IndexController@action'); }); 

Or so, if only one

 Route::post('/action', 'IndexController@action')->middleware('throttle:1,0.03'); 
  • very useful, thank you, but how to do it once every 2 seconds through it? - Roman Kashirov
  • @RomanKashirov know how to share? - n.osennij
  • Thanks, buddy - Roman Kashirov
  • No, wait. My Ajax sends data: type for a single function via Route::post('action', 'IndexController@action'); Further in the function if($r->type == betMin) or if(($r->type == betMax) . That is, I need to make flood control on specific lines in the function - Roman Kashirov
  • Well, divide the routes. Each class method can be a separate route. I have no idea what you are doing and how. - n.osennij