Laravel 9 How To Block Whitelist IP Address Tutorial

Reading Time: 4 minutes
1,154 Views

Inside this article we will see the concept i.e Laravel 9 How To Block Whitelist IP Address. Article contains the classified information about block or whitelist IPs in Laravel. There is very simple process using the concept of middleware by which we can restrict access to specific IPs.

If you are looking for a solution to block or whitelist IPs in laravel application then this article will help you a lot for this. You will learn to restrict IPs for your laravel application. We will use the concept of Laravel middleware.

Sometimes, you want to restrict or block specific IP addresses to access your website. So, In this article you will see how to create middleware and block ip addresses to access URLs.

Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Middleware

Open project into terminal and run this command to create middleware. This middleware we will use to block or restrict user access via IPs.

$ php artisan make:middleware BlockIpMiddleware

It will create a middleware file BlockIpMiddleware.php inside /app/Http/Middleware folder.

Open file and write this code into it.

<?php
  
namespace App\Http\Middleware;
  
use Closure;
use Illuminate\Http\Request;
  
class BlockIpMiddleware
{
    // Provide the IP list to be restrict  
    public $blockIps = ['whitelist-ip-1', 'whitelist-ip-2', 'whitelist-ip-3', '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);
    }
}

Register BlockIP Middleware

Open Kernel.php file from /app/Http folder. Search for $routeMiddleware. Add this line into it.

//...

protected $routeMiddleware = [
    
    ....

    'blockIP' => \App\Http\Middleware\BlockIpMiddleware::class,
];

//...

Now, we have a route middleware which restricts the IPs to access site.

Route Middleware Usage

Next,

We will use this route middleware to application routes to restrict IPs for those application URLs.

We can use in web.php as:

//...
  
use App\Http\Controllers\RSSFeedController;
use App\Http\Controllers\UserController;
   
//...

Route::middleware(['blockIP'])->group(function () {
    Route::resource('users', UserController::class);
    Route::resource('rss', RSSFeedController::class);
});

//...

We hope this article helped you to learn Laravel 9 How To Block Whitelist IP Address Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.