How To Get URL of Current Page in PHP

PHP How To

There are a lot of situations where you might need to get the URL of a webpage that any user might be currently visiting. For example, you may require you to dynamically generate the title of a webpage based on its URL. There are many ways in which you can obtain the URL of a webpage.

images/articles/php/get-url-of-current-page-in-php.jpg

Get the Full URL with Query String

Using $_SERVER['REQUEST_URI']

The $_SERVER superglobal array in PHP contains a lot of useful information like the headers, timestamp for the request, paths and script locations.

You can use the value of $_SERVER['HTTP_HOST'] to get the contents of the Host: header for current request. In other words, $_SERVER['HTTP_HOST'] returns the domain name of the current page.

Similarly, you can use $_SERVER['REQUEST_URI'] to get the URI that was given in order to access the current page. This value will include the query string of the URL as well.

The only thing that you need to know now is if the URL was served over HTTP or HTTPS. This can be done by checking the value of $_SERVER['HTTPS']. When a page is served over HTTPS, this element is set to an non-empty value. However, there is a catch. When using ISAPI with IIS, the value of this element will be set to off. What you need to do in this situation is check if a value has been set and verify that it is not equal to off.

Example:

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $full_url;

The URL contains all the parameters of the query string. The only problem is that you cannot get the value of any fragment identifier present in the URL. This is because the fragment identifier is not passed to the server at all. If you need access to the fragment identifier you will have to do it client-side using JavaScript.

Get the Full URL Without Query String

Using $_SERVER[‘SCRIPT_URI’]

If you have enabled mod_rewrite, you can also use $_SERVER['SCRIPT_URI'] to get the whole URL at once. The only drawback of this method is that you will get the URL without the query string.

$full_url = ['SCRIPT_URI'];
echo $full_url;

Remove Query String From the URL

Using str_replace()

You have the whole URL of the current page. Now, you remove the query string using inbuilt PHP functions.

You can easily obtain the query string of a URL, with the help of $_SERVER['QUERY_STRING']. Once you have the query string, you can use the string_replace() function in PHP to replace it with an empty string.

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';

$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$query_string = $_SERVER['QUERY_STRING'];
$params = '?'.$query_string;

$no_params_url = str_replace($params, '', $_SERVER[REQUEST_URI]);

Remove Query String From the URL

using explode()

Another PHP function that you can use to get rid of the query string in a URL is explode(). The explode() function splits a string into an array of smaller substrings using given delimiter.

To remove the query string in our URL, you can use the full URL as the main string and a ‘?’ as the delimiter.

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';

$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$no_params_url = explode('?', $full_url, 2)[0];

The explode() function splits $full_url into smaller components which stored as array elements. The URL without the query string will be stored in the first element of the resulting array. You just access it and assign the value to $no_params_url.

Remove Parameters From the URL

Using strtok()

Some people might not prefer the explode() function to remove the parameters in a URL because it returns an array and they have to access the first element of that array in order to get the URL.

You can also remove any parameters from the full URL string using the strtok() function in PHP. This function splits a string into smaller tokens but it returns a string instead of an array.

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';

$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$no_params_url = strtok($full_url, '?');