How can the source be extracted from a header("Location: ...") in PHP?

When using the header("Location: ...") function in PHP to redirect a user to another page, the source URL is not directly accessible in the script. To extract the source URL, you can use the $_SERVER['HTTP_REFERER'] variable, which contains the previous page's URL. This can be useful for logging or tracking purposes.

// Extract the source URL from the HTTP_REFERER header
$sourceURL = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Unknown';

// Output the extracted source URL
echo "Source URL: " . $sourceURL;