In what ways can PHP scripts be optimized for dynamic redirection to external pages while ensuring user awareness of the source?

When dynamically redirecting users to external pages in PHP scripts, it is important to ensure that users are aware of the source of the redirect to prevent phishing attacks. One way to achieve this is by adding a parameter to the URL that indicates the source of the redirect, such as a token or a session variable. This way, users can see where they are being redirected from before proceeding to the external page.

<?php
// Set the source of the redirect
$source = "example.com";

// Redirect to the external page with the source parameter
header("Location: https://externalpage.com?source=" . $source);
exit;
?>