How Can I Get the Full URL of Current Page in PHP?
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 instance, a project may require you to dynamically generate the title of a webpage based on its URL. There are many ways in which we can obtain the URL of a webpage and we will discuss them all in detail in this tutorial.
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. For this particular tutorial, we will be looking into several indices of the $_SERVER
array that can be used to construct the full URL of the current page with the parameters.
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 we 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 we need to do in this situation is check if a value has been set and verify that it is not equal to off
.
The example below provides a simple solution to get the full URL of current page the user is visiting with the query string intact.
PHP
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $full_url;
I have tested the above code on a test page that I created and it produced the following output.
PHP
URL in address bar — https://tutorialio.com/php/faqs/index.php
URL output by above code — https://tutorialio.com/php/faqs/index.php
URL in address bar — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
URL output by above code — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
URL in address bar — https://tutorialio.com/php/faqs/index.php?page=12&sort=views#something
URL output by above code — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
As you can see, the URL contains all the parameters of the query string. The only problem is that we 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. However, if you were planning to discard the query string anyway, this is an elegant one line solution.
PHP
$full_url = ['SCRIPT_URI'];
echo $full_url;
/* URL in address bar — https://tutorialio.com/php/faqs/index.php
URL output by above code — https://tutorialio.com/php/faqs/index.php
URL in address bar — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
URL output by above code — https://tutorialio.com/php/faqs/index.php
URL in address bar — https://tutorialio.com/php/faqs/index.php?page=12&sort=views#something
URL output by above code — https://tutorialio.com/php/faqs/index.php */
Remove Query String From the URL Using str_replace()
We already have the whole URL of the current page thanks to the code we wrote in first section. Even though we got the URL without the query string in the second section, that method only works if mod_rewrite has been enabled. If you can’t make any assumptions about mod_rewrite, you should get the whole URL and then 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. The following example should make it clear.
PHP
// Test URL — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
// $full_url = https://tutorialio.com/php/faqs/index.php?page=12&sort=views
$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// $query_string = page=12&sort=views
$query_string = $_SERVER['QUERY_STRING'];
$params = '?'.$query_string;
// $no_params_url = https://tutorialio.com/php/faqs/index.php
$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 basically splits a string into an array of smaller substrings using given delimiter.
To remove the query string in our URL, we can use the full URL as the main string and a ‘?’ as the delimiter. The following code snippet will make things more clear:
PHP
// Test URL — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
// $full_url = https://tutorialio.com/php/faqs/index.php?page=12&sort=views
$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// $no_params_url = https://tutorialio.com/php/faqs/index.php
$no_params_url = explode('?', $full_url, 2)[0];
A lot is going on in the last line of our code, 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. We 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 also splits a string into smaller tokens but it returns a string instead of an array.
PHP
// Test URL — https://tutorialio.com/php/faqs/index.php?page=12&sort=views
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
// $full_url = https://tutorialio.com/php/faqs/index.php?page=12&sort=views
$full_url = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// $no_params_url = https://tutorialio.com/php/faqs/index.php
$no_params_url = strtok($full_url, '?');
Quick Summary
Let’s recap everything that we have covered in this tutorial.
- You can get the full URL of current page along with the query string, using a combination of
$_SERVER["HTTP_HOST"]
and$_SERVER["REQUEST_URI"]
. - If you want the URL without the query string, the easiest way to get it will be with
$_SERVER["SCRIPT_URI"]
. However, this will only work ifmod_rewrite
has been enabled. - If you can’t rely on
mod_rewrite
, the best way to get the URL of current page without the query string would be to first get the full URL. Once you have the full URL, you can use functions likestr_replace()
,strtok()
andexplode()
to remove the query string.
Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to get the full URL of current page with or without the query string in PHP.
Rate this post —