How can the full URL be displayed when using header() in PHP?

When using the header() function in PHP to redirect to a new URL, the full URL may not be displayed if only a relative path is provided. To display the full URL, you need to include the protocol (http:// or https://) and the domain name in the URL. This can be achieved by using the $_SERVER['HTTP_HOST'] variable to dynamically get the domain name and concatenating it with the desired path.

// Get the current protocol
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://";

// Get the domain name
$domain = $_SERVER['HTTP_HOST'];

// Redirect to the full URL
header("Location: " . $protocol . $domain . "/path/to/redirect");