What are the best practices for handling maintenance mode in PHP applications to ensure minimal disruption to users and maximum security?

When a PHP application is in maintenance mode, it is important to ensure minimal disruption to users and maximum security. One best practice is to create a maintenance mode page that informs users of the temporary downtime and prevents unauthorized access to the application. This can be achieved by checking a maintenance mode flag in the application configuration and redirecting users to the maintenance page if the flag is set.

// Check if maintenance mode is enabled
if ($maintenance_mode) {
    header('HTTP/1.1 503 Service Unavailable');
    header('Status: 503 Service Unavailable');
    include('maintenance_page.php');
    exit;
}