How To Block IP Address In Laravel 10

Websolutionstuff | May-17-2023 | Categories : Laravel

In this article, we will see how to block IP addresses in laravel 10. Here we will learn about how to restrict user access from IP addresses in laravel 10. 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 10. Also, we will see how to create middleware and block IP addresses to access URLs.

So, let's see laravel 10 block IP address, how to restrict user access from IP address, laravel 10 restrict user access from IP address, laravel restrict IP address, how to get a user's IP address in laravel 10, and how to block country in laravel 10.

Step 1: Install Laravel 10

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

composer create-project laravel/laravel laravel_10_example

 

Step 2: Create Middleware

Then, we will create a BlockIPAddressMiddleware file using the following command and update the below code to that file.

php artisan make:middleware BlockIPAddressMiddleware

app/Http/Middleware/BlockIPAddressMiddleware.php

<?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);
    }
}

 

 

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 10 restrict user access from the IP address using the following command.

php artisan serve

Output:

how_to_block_ip_address_in_laravel_10_output

 


You might also like:

Recommended Post
Featured Post
How to Create a Livewire Image Upload in Laravel 10?
How to Create a Livewire Image...

Livewire has revolutionized the way developers build interactive web applications with its real-time capabilities. And w...

Read More

Aug-02-2023

How To Validate Email Using jQuery
How To Validate Email Using jQ...

In this article, we will see how to validate email using jquery. we will use regular expression(regex) for email va...

Read More

Nov-09-2022

Laravel 10 Custom Validation Error Message
Laravel 10 Custom Validation E...

In this article, we will see the laravel 10 custom validation error message. Here, we will learn about how to creat...

Read More

May-19-2023

jQuery Datatable Hide/Show Column Based On Condition
jQuery Datatable Hide/Show Col...

In this article, we will see a jquery datatable hide/show column based on condition. Here, we will learn how to hide and...

Read More

Jan-26-2023