Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ricardo's avatar

Middleware to White list IPs while app is down

Some times we need to do some verifications while the Applications is Down, here is an implementation.

In your .env file add:

WHEN_DOWN_WHITELIST_THIS_IPS=your_pulic_ip,other_ip

add a middleware

public function __construct(Application $app)
{
    $this->app = $app;
}
...
public function handle($request, Closure $next)
{
    if ($this->app->isDownForMaintenance() && ! $this->isIpWhiteListed()) {
        throw new HttpException(503);
    }

    return $next($request);
}

private function isIpWhiteListed()
{
    $ip = Request::getClientIp();
    $allowed = explode(',', getenv('WHEN_DOWN_WHITELIST_THIS_IPS'));

    return in_array($ip, $allowed);
}

comment the Laravel 5 default chek for maintenance mode in the Kernel and add your own.

    // 'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'App\Http\Middleware\CheckForMaintenanceMode',
0 likes
0 replies

Please or to participate in this conversation.