What is the best practice for implementing automatic redirection in PHP after password input?

When implementing automatic redirection in PHP after password input, it is important to ensure that the redirection is secure and efficient. One common approach is to use the header() function to send a Location header with the URL to redirect to. This should be done after verifying the password input and before any output is sent to the browser.

<?php
// Check password input
if ($password === 'correct_password') {
    // Redirect to a secure page
    header('Location: secure_page.php');
    exit;
} else {
    // Redirect to an error page
    header('Location: error_page.php');
    exit;
}
?>