Skip to content
PHP Logo

How can the EVA principle be applied in PHP to ensure proper header redirection?

When performing header redirection in PHP, it is important to follow the EVA principle (Exit early, Validate input, Avoid duplication) to ensure proper redirection. This means that we should exit the script after performing the redirection, validate the input to prevent any malicious redirection, and avoid duplicating the redirection logic in multiple places.

php
// Validate input
if (isset($_GET['redirect'])) {
    $redirect_url = $_GET['redirect'];
    // Perform any additional validation here if needed
} else {
    // Handle invalid input
    die("Invalid redirect URL");
}

// Perform header redirection
header("Location: $redirect_url");
exit; // Exit early to prevent further execution