How can the EVA principle be applied to prevent header modification errors in PHP?

Header modification errors in PHP can be prevented by following the EVA principle, which stands for Escape, Validate, and Authenticate. This means escaping any user input before using it to modify headers, validating the input to ensure it meets expected criteria, and authenticating the user to prevent unauthorized modifications. By applying these principles, the risk of header modification errors can be greatly reduced.

// Escape user input before using it to modify headers
$user_input = htmlspecialchars($_POST['user_input']);

// Validate the input to ensure it meets expected criteria
if (filter_var($user_input, FILTER_VALIDATE_EMAIL)) {
    // Authenticate the user to prevent unauthorized modifications
    if ($user_authenticated) {
        header("Location: https://example.com");
        exit();
    } else {
        echo "Unauthorized access.";
    }
} else {
    echo "Invalid email address.";
}