Get the full URL in PHP

Created by ax.Linked to 13.4m issues across 239 teams

tl;dr

Here's how to get the full URL in PHP:

First, you need to use the $_SERVER['REQUEST_URI'] variable. This will give you the URL of the current page.

To get the full URL, you need to combine the $_SERVER['HTTP_HOST'] variable with the $_SERVER['REQUEST_URI'] variable. This can be done with the following code:

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

If you want to support both HTTP and HTTPS, you can use the following code:

$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

It is important to note that using this code has some security implications since the client can set HTTP_HOST and REQUEST_URI to arbitrary values. It's very important to sanitize both values and do meaningful input validation.