Laravel Block/Whitelist IP Address Tutorial
Hello Dev,
If you need to see an example of laravel restrict user access from ip. This article goes into detail on laravel restrict ip address to access user. you can understand a concept of laravel blacklist ip middleware. let’s discuss laravel middleware ip whitelist. Let's see bellow example laravel middleware block ip.
you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version as well.
Sometimes, we want to restrict or block specific IP addresses to access our website. in this tutorial I will show how to create middleware and block ip addresses to access URLs.
In this tutorial example, we will create one middleware as "BlockIpMiddleware" and we will use that middleware on every secure api and url. So just see bellow steps how to complete this things:
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Create Middleware
In this step, open terminal and run below command to create BlockIpMiddleware middleware file, so let's run below command:
php artisan make:middleware BlockIpMiddleware
Now, it's created new BlockIpMiddleware.php file. you have to add block ips on $blockIps array list. let's update following code on this file.
app/Http/Middleware/BlockIpMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class BlockIpMiddleware
{
public $blockIps = ['whitelist-ip-1', 'whitelist-ip-2', '127.0.0.1'];
/**
* 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 file, we need to register middleware on Kernel.php file. we will call blockIP of new created middleware. so let's update following 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 = [
....
'blockIP' => \App\Http\Middleware\BlockIpMiddleware::class,
];
}
Step 4: Use Middleware
In this step, we will create one route and show you how to use middleware in route file. so let's open your route file and update following code:
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RSSFeedController;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| 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(['blockIP'])->group(function () {
Route::resource('users', UserController::class);
Route::resource('rss', RSSFeedController::class);
});
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/users
You will find following layout:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to install and use Image Intervention in Laravel?
- How to Create Custom Middleware in Laravel?
- Laravel 9 Generate Sitemap XML File Tutorial Example
- Laravel Contact Form Send Email Tutorial
- Laravel Webcam Capture Image and Save from Camera Example
- Laravel 9 Multiple Database Connections Example
- Laravel JQuery UI Autocomplete Ajax Search Example
- How to Generate Random Unique Number in Laravel?
- How to Call Middleware from Controller in Laravel?
- Laravel User Access Control using Middleware Example
- Laravel Get Route Parameters in Middleware Example
- Laravel XSS Protection Middleware Example