Home Laravel Laravel 9 Restrict User Access From IP Address

Laravel 9 Restrict User Access From IP Address

by Shahriar Sagor

Today now in this post i will cover an laravel 9 restrict user access from ip. So it’s a very simple example of laravel 9 restrict ip address to access user. Now in this article i will goes into detail on laravel 9 blacklist IP middleware. So now i would like to share with you an example of laravel 9 middleware ip whitelist. Just need to follow the bellow tutorial step of laravel 9 middleware block IP.

Step 1: Install Laravel

At first of all we have to get the fresh Laravel version application by using the bellow command. So open your terminal OR command prompt and then run the bellow command:

composer create-project laravel/laravel example-app
Step 2: Create Middleware

Now in this step, need to open terminal and then run the below command to create BlockIpMiddleware middleware file, so let’s run the below command:

php artisan make:middleware BlockIpMiddleware

Now, it will created a new BlockIpMiddleware.php file. I have to add the block ips on $blockIps array list. let’s need to update following code on this file.

app/Http/Middleware/BlockIpMiddleware.php

<?php
  
namespace App\Http\Middleware;
  
use Closure;
use Illuminate\Http\Request;
  
class BlockIpMiddleware
{
    public $blockIps = ['whitelist-ip-1', 'whitelist-ip-2', '127.0.0.1'];
  
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if (in_array($request->ip(), $this->blockIps)) {
            abort(403, "You are restricted to access the site.");
        }
  
        return $next($request);
    }
}

Step 3: Register Middleware

Now in this file, i have to register the middleware on Kernel.php file. I will call the blockIP of new created middleware. So let’s update the following file.

app/Http/Kernel.php

<?php
  
namespace App\Http;
  
use Illuminate\Foundation\Http\Kernel as HttpKernel;
  
class Kernel extends HttpKernel
{
    ....
  
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        ....
        'blockIP' => \App\Http\Middleware\BlockIpMiddleware::class,
    ];
}

Step 4: Use Middleware

Now in this step, i will create one route and then show you how to use the middleware in route file. So let’s open your route file and then update the following code:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\RSSFeedController;
use App\Http\Controllers\UserController;
   
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
    
Route::middleware(['blockIP'])->group(function () {
    Route::resource('users', UserController::class);
    Route::resource('rss', RSSFeedController::class);
});

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like