Use the one built into Laravel.
Jan 18, 2025
18
Level 17
Rate limiter not working properly.
I have setup a custom rate limiter in laravel 11.
it looks like it should work but it's not.
public function DashboardVal(Request $request)
{
$request->validate([
'message' => ['max:124']
]);
$maxAttempts = 5; // Allow 5 requests per two minutes (adjust as needed)
$key = 'latest-status:' . Auth::id();
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
$seconds = RateLimiter::availableIn($key);
return response()->json([
'message' => 'Too many requests. You may try again in '.$seconds.' seconds.',
'type' => 'error'
], 429);
}
RateLimiter::increment($key);
if ($request->message != Auth::user()->latestStatus) {
// Logging
$status = new Status;
$status->creator_id = Auth::id();
$status->message = $request->message;
$status->save();
$user = User::find(Auth::id());
$user->status = $request->message;
$user->save();
return response()->json(
[
'message' => 'Your status has been changed.',
'type' => 'success'
]
);
}
}
what should I do?
Level 17
@benridev well i think i figured this out. https://stackoverflow.com/questions/33268683/how-to-get-client-ip-address-in-laravel-5/41769505#41769505
now the question is how do i extend Illuminate\Http\Request to implement this method for the load balancer,
"To fix this:
Illuminate\Http\Request.php
public function ip()
{
//return $this->getClientIp(); //original method
return $this->getIp(); // the above method
}
```"
Please or to participate in this conversation.