How To Restrict User Access From IP Address Laravel

  • 02-09-2022
  • 1522
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

How to restrict user access via IP address in laravel 9; to getting restrict use based on users IP address who come with specific IP address which you don't want to access some HTTP request in your app that time this post will help you to restrict IP address for the specific user.

Laravel restricts user access from IP addresses; we will take the help of middleware and apply on that route which you want to restrict for a specific IP address. So in this post, I created a middleware and gave an example of how to restrict some pi addresses for a specifica route.

Laravel middleware IP whitelist, after creation we will register that middleware in kernel file, then after you can you that restricted IP address middleware by applying middleware name to the route file. You can also apply to the whole route group or all web HTTP requests by registering in web routes in the kernel file.

Preview:
image

Use the below command and run it in your terminal, This command will generate a middleware file in your application. In this middleware, we just make a simple logic for restriction IP address.

php artisan make:middleware RestrictIPMiddleware

Goto generated middleware and apply logic as per the below example give, You can copy the whole middleware and paste into your application also.

Here, In this middleware, you can see an array of IP addresses, so in this case, I want to restrict this IP address which I added to the array. So whenever any user comes with a given IP address, the user is not able to allow the specific request.

app/Http/Middleware/RestrictIPMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;

class RestrictIPMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $restrictIp = [
            '127.0.0.1',
            '127.0.0.1',
            '204.85.191.9',
            '205.170.62.207',
            '205.185.115.33',
            '205.209.253.161',
            '207.255.225.253',
            '209.141.57.178',
            '212.227.115.239',
            '213.202.223.95',
            '217.79.179.7',
        ];

        if (in_array($request->ip(),$restrictIp)) {
            
            abort(403, 'You are not authorized to access this site.');
        }

        return $next($request);
    }
}
    

Register created middleware in this kernel file as per the below given example.

app/Http/Kernel.php
protected $routeMiddleware = [
    .......
    'ip.restrict' => \App\Http\Middleware\RestrictIPMiddleware::class,
];

This is our route file, we will apply the middleware name after the end of the route and generate middleware. apply restrict IP middleware on the route which you want to restrict for a specific route.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;

/*
|--------------------------------------------------------------------------
| 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::get('dashboard',[AdminController::class,'dashboard'])->middleware('ip.restrict');
Route::get('contact-us',[AdminController::class,'contactUs'])->middleware('ip.restrict');;
Route::get('latest-news',[AdminController::class,'latestNews']);

How To Secure Laravel Website From Hackers


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :