Laravel - Block Restrict User From IP Address in Middleware

Sovary October 19, 2022 514
2 minutes read

Today tutorial we will learn how to blacklist Ip address user for accessing website in Laravel. We will show you how to block restrict users by IP address from accessing the website.

In some cases, you may want to block a specific IP address to access your website, so this tutorial will explain and show you step by step to restrict access user by blacklist their IP address in laravel middleware.

Firstly, we will create custome middleware in Laravel and the middleware will filter specific IP address which requested by users. If we have bad experience with some user attempt login or access your website this middleware will reject the request via the requested IP address.

Block User Access From IP Addresses Example

Following steps we will go through to achieved this topic

  • Step 1 - Install Laravel
  • Step 2 - Create Middleware
  • Step 3 - Register Middleware
  • Step 4 - Run Laravel Server

Step 1 - Install Laravel

We are going to download fresh project by command, let's open your command prompt or terminal to run the command below. In case you want to read detail install laravel from scratch you may read this article. 

composer create-project laravel/laravel blog

Step 2 - Create Middleware

Now this step we have to create new middleware file to implement the logic filter out IP address request from users. Let's run command:

php artisan make:middleware IpBanMiddleware

After run command, the file will generated in under Middleware folder.

Open file App -> Http -> Middleware -> IpBanMiddleware.php

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Auth;

class IpBanMiddleware
{
    // set IP addresses
    public $blacklist = ['195.165.16.42', '192.8.0.2', '127.0.0.1'];
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  \Integer  role
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (in_array($request->ip(), $this->blacklist)) 
        {
            return response()->json(['msg' => "Ban Access "]);
        }
        return $next($request);
    }
}

Step 3 - Register Middleware

Thes stpes will registeor the middleware to kernel file. Let's go to command promopt and replace the register as below example.

Open file app -> Http -> Kernel.php

protected $middlewareGroups = [
    'web' => [
        ...
        \App\Http\Middleware\IpBanMiddleware::class,
    ],
    'api' => [ ...  ],
];

Step 4 - Run Laravel Server

Last, we will run command to start Laravel project by command:

php artisan serve

Then launch browser and go to URL http://localhost:8000/ you can try with your own IP address to test if it working or not.

You might also like...

 

Laravel  PHP  Laravel 9  Laravel Middleware 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him