How To Restrict User Access From IP Address In Laravel 9

    In this article, we will see how to restrict user access from IP addresses in laravel 9. Here, we will learn how to block IP addresses in laravel 7, laravel 8, and laravel 9. Sometimes we are required to restrict or block users using IP addresses or block from selected countries at that time you can create a blocklist of IP addresses or only allow a whitelist of IP addresses.

    We use middleware to restrict or block the user's IP address in laravel 7, laravel 8, and laravel 9.

    So, let's see the laravel 9 restrict user access using IP address, laravel 9 block countries, laravel 9 block IP address, and laravel restrict IP address.

    Step 1: Install Laravel 9 Application

    Step 2: Create Middleware

    Step 3: Register Middleware

    Step 4: Use Middleware

    Step 5: Run Laravel Application

Step 1: Install Laravel 9 Application

    In this step, we will install the laravel 9 application using the following command.

composer create-project laravel/laravel laravel_9_IP_Address
Step 2: Create Middleware

    Now, we will create a BlockIPAddressMiddleware file using the following command.

php artisan make:middleware BlockIPAddressMiddleware

    Now, open the app/Http/Middleware/BlockIPAddressMiddleware.php file and update the below code.

<?php
  
namespace App\Http\Middleware;
  
use Closure;
use Illuminate\Http\Request;
  
class BlockIPAddressMiddleware
{
    public $blockIPs = ['Block-IP-1', 'Block-IP-2', 'Block-IP-3'];
  
    /**
     * 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);
    }
}
Read Also: Laravel 9 Group Column Chart Using Highcharts
Step 3: Register Middleware

    In this step, we will register the middleware file to the kernel.php file. So, add the following code to that 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 = [
        ....
        'blockIPAddress' => \App\Http\Middleware\BlockIPAddressMiddleware::class,
    ];
}
Step 4: Use Middleware

    Now, we will use the BlockIPAddressMiddleware in the route file. So, update the web.php file.

    routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;  
use App\Http\Controllers\PostController;
   
/*
|--------------------------------------------------------------------------
| 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(['blockIPAddress'])->group(function () {
    Route::resource('users', UserController::class);
    Route::resource('post', PostController::class);
});
Step 5: Run Laravel Application

    Now, run the laravel 9 restrict user access from the IP address using the following command.

php artisan serve

    Output:

    laravel_9_restrict_user_access_from_ip_address

    You might also like:

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan