Laravel Block/Whitelist IP Address Tutorial

By Hardik Savani April 16, 2024 Category : PHP Laravel

Hello Dev,

If you need to see an example of laravel restrict user access from ip. This article goes into detail on laravel restrict ip address to access user. you can understand a concept of laravel blacklist ip middleware. let’s discuss laravel middleware ip whitelist. Let's see bellow example laravel middleware block ip.

you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version as well.

Sometimes, we want to restrict or block specific IP addresses to access our website. in this tutorial I will show how to create middleware and block ip addresses to access URLs.

In this tutorial example, we will create one middleware as "BlockIpMiddleware" and we will use that middleware on every secure api and url. So just see bellow steps how to complete this things:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Middleware

In this step, open terminal and run below command to create BlockIpMiddleware middleware file, so let's run below command:

php artisan make:middleware BlockIpMiddleware

Now, it's created new BlockIpMiddleware.php file. you have to add block ips on $blockIps array list. let's 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

In this file, we need to register middleware on Kernel.php file. we will call blockIP of new created middleware. so let's update 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

In this step, we will create one route and show you how to use middleware in route file. so let's open your route file and update 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);

});

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/users

You will find following layout:

I hope it can help you...

Shares