How To Restrict User Access From IP Address In Laravel 9

Websolutionstuff | Jan-03-2023 | Categories : Laravel

Imagine this: You've made a super cool website, and now you want to make sure only the right people can use it. That's where the IP address trick comes in!

In this article, we're like digital detectives exploring Laravel 9. We're going to learn how to control who can use your website based on their IP addresses.

Together, we'll go through each step. By the end, you'll know how to keep your website safe, make it more secure, and let only the people you want use it.

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

 

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

 

Conclusion

Restricting user access based on IP addresses in Laravel 9 offers heightened security and control to your web application. By implementing these steps, you empower yourself to safeguard sensitive data and confidential information effectively.

With our guide, you're now equipped to protect your digital domain against unauthorized access. As the digital landscape continues to evolve, this method remains a pivotal tool for maintaining the integrity of your online platform.


You might also like:

Recommended Post
Featured Post
How To Install Vue 3 In Laravel 9 With Vite
How To Install Vue 3 In Larave...

In this article, we will see how to install Vue 3 in laravel 9 with vite. In the previous article, we will install...

Read More

Oct-10-2022

Laravel 11 Create CRUD Operation with Database
Laravel 11 Create CRUD Operati...

Hello developers! In this article, we'll learn about Laravel 11 to create Creat Reas Update Delete operations w...

Read More

Apr-08-2024

How to Use Laravel Factory in Seeder
How to Use Laravel Factory in...

Laravel is a popular PHP framework known for its elegance and simplicity in building web applications. When it comes to...

Read More

Sep-13-2023

How to Install Node JS and NPM on Ubuntu 22.04
How to Install Node JS and NPM...

Hey fellow Ubuntu enthusiasts! 🐧✨ If you're looking to step into the awesome world of Node.js and npm for your web d...

Read More

Jan-12-2024