How can the header function in PHP be used to properly handle page redirection after a successful login attempt?

To handle page redirection after a successful login attempt in PHP, you can use the header function to send a raw HTTP header to the browser, which will redirect the user to a new page. After verifying the login credentials, you can use the header function with the Location parameter set to the desired redirect URL. This will ensure that the user is redirected to the appropriate page upon successful login.

// Check login credentials
if ($login_successful) {
    // Redirect to the home page after successful login
    header("Location: home.php");
    exit();
}