Hello Devs,

In this tutorial, we are going to learn how to access particular ip address laravel application.

Follow this step by step guide given below:




Create a Middleware

php artisan make:middleware CheckIpMiddleware

app/Http/Middleware/CheckIpMiddleware

<?php

namespace App\Http\Middleware;

use Closure;

class CheckIpMiddleware
{
    // set IP addresses
    public $restrictIps = ['ip-addr-0', 'ip-addr-1', '127.0.0.5'];

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (in_array($request->ip(), $this->restrictIps)) {
            return response()->json(['message' => "You don't valid Ip Address"]);
        }

        return $next($request);
    }
}

app/Http/Middleware/Kernel.php

protected $middlewareGroups = [
    'web' => [
        //--------------
        \App\Http\Middleware\CheckIpMiddleware::class,
    ],

    'api' => [
        //--------------
    ],
];



Test

You don't valid Ip Address


I hope this example helps you.