In Laravel, you can restrict user access from specific IP addresses by creating a middleware that checks the user’s IP address and denies access if it is on a blocked list.
Here are the steps to create a middleware to block access from specific IP addresses:
php artisan make:middleware BlockIp
Here is an example of what the handle method in the middleware file might look like
public function handle($request, Closure $next)
{
$ip = $request->ip();
$blockedIps = ['127.0.0.1', '192.168.0.1'];
if (in_array($ip, $blockedIps)) {
return response()->json(['error' => 'Access denied.'], 403);
}
return $next($request);
}
To add the middleware to a specific route or controller, you can use the middleware
method. For example, if you want to add this middleware to a route group
Route::middleware(['auth','blockip'])->group(function () {
Route::get('/dashboard', 'DashboardController@index');
});
It will block access to the ‘/dashboard’ route for the IP addresses specified in the middleware
In September 2023, Wesley Wang uploaded a YouTube video titled "nothing except everything," which unexpectedly…
Alexia Tabone is a highly respected neuroscientist known for her groundbreaking research in the field…
भक्तपुरमा नौ दिन आठ रातसम्म परम्परागत विधिअनुसार मनाइने विश्वप्रसिद्ध बिस्का: जात्रा आज चैत २७ गते…
The Woman Who Invented WI-FI. The most beautiful woman in the world in cinema and…
“यस क्षण” कुनै सोच होइन । यो एक मात्र वास्तविकता हो तपाईं आफूलाई अरूसँग तुलना…
Dear SEE Students: तपाईंको नतिजाको Grading system यस्तो रहनेछ ! कुनै पनि विद्यार्थी फेल हुने…
This website uses cookies.