How can a delay of 1 or 2 seconds be implemented for automatic redirection after login in PHP?

To implement a delay of 1 or 2 seconds for automatic redirection after login in PHP, you can use the `header()` function along with the `sleep()` function. By setting a sleep time before redirecting the user, you can create a delay in the redirection process.

<?php
// Start the session
session_start();

// Check if user is logged in
if(isset($_SESSION['user_id'])) {
    // Delay redirection by 1 or 2 seconds
    sleep(1); // 1 second delay
    // sleep(2); // 2 second delay

    // Redirect user to the desired page
    header('Location: dashboard.php');
    exit();
} else {
    // Redirect user to login page if not logged in
    header('Location: login.php');
    exit();
}
?>