What are best practices for implementing a maintenance mode feature in a PHP website with user authentication?

When implementing a maintenance mode feature in a PHP website with user authentication, it is important to ensure that only administrators can access the site during maintenance. This can be achieved by checking the user's role or permissions before allowing access. Additionally, it is recommended to display a user-friendly maintenance message to non-admin users.

// Check if user is an administrator before allowing access during maintenance mode
if($user->role != 'admin'){
    echo "Sorry, the website is currently undergoing maintenance. Please try again later.";
    exit;
}

// Code to display the website content for administrators