Posts

Showing posts with the label block IPs

Laravel Middleware - Block IPs using Middleware class

When talking about securing the Laravel application, we might also want to restrict IP address to access our application, especially when we have Laravel setup as a Backend server providing data through APIs or web services. As of today, you might find no. of Laravel package providing IP address restriction functionality but I found it does not make any sense to install a package when it is achieved through only One class.  Yes! we can just do this with a simple middleware class.  Let me explain to you little details on how we can achieve it using the Middleware class. First, let's create a middleware class using the Laravel artisan command: php artisan make:middleware RestrictIPAddress It will create a new class in your Laravel project under the ` app/Http/Middleware ` directory. It will have a default empty method handle(), where you have to write your logic. Logic is simple, you just have to get the IP address from where the request is coming. here is the method...