What is the issue with automatically redirecting a PHP page to an external site?

Automatically redirecting a PHP page to an external site can pose security risks, as it can potentially lead to phishing attacks or other malicious activities. To solve this issue, it is recommended to validate the URL being redirected to and ensure it is a trusted source before performing the redirect.

// Validate the URL before redirecting
$redirect_url = 'https://example.com';
if (filter_var($redirect_url, FILTER_VALIDATE_URL)) {
    header("Location: $redirect_url");
    exit();
} else {
    echo "Invalid URL";
}