How can relative URLs be converted to absolute URLs for use with the header() function in PHP?

When using the header() function in PHP to redirect to a URL, it is important to provide an absolute URL rather than a relative URL. To convert relative URLs to absolute URLs, you can use the $_SERVER['HTTP_HOST'] variable to get the current domain and concatenate it with the relative URL. This ensures that the redirect works correctly across different pages and domains.

$relative_url = "/example.php";
$absolute_url = "http://".$_SERVER['HTTP_HOST'].$relative_url;
header("Location: $absolute_url");
exit();