How to Find the Current URL with PHP

The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are:

1. The domain name: www.example.com
2. The path to the page: /example/page.php
3. The query string: name=Bob

So how do you find it all out with your own PHP scripts?

All of the information we need is stored in the $_SERVER array, which is accessible from anywhere in your PHP script (and as such is called a superglobal variable), it works like a normal array and the keys we wish to retrieve the values of are 'HTTP_HOST', 'SCRIPT_NAME' and 'QUERY_STRING' for the three different parts of the url. Alternatively, if we don't need to have the path to the page and the query string seperate, we can use 'REQUEST_URI'.

The following code should let you find it:

<?php
// find out the domain:
$domain = $_SERVER['HTTP_HOST'];
// find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];
// put it all together:
$url = "http://" . $domain . $path . "?" . $queryString;
echo "The current URL is: " . $url . "
";

// An alternative way is to use REQUEST_URI instead of both
// SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
$url2 = "http://" . $domain . $_SERVER['REQUEST_URI'];
echo "The alternative way: " . $url2;
?>

Related Posts:

7 Responses to “How to Find the Current URL with PHP”



  • You might want to get the https as well.

    $http = ($_SERVER['HTTPS'] ? 'https://' : 'http://');

    $url2 = $http . $domain . $_SERVER['REQUEST_URI'];


  • How about anchor link? #anchor


  • thanks dear,
    very very thanking you.


  • These techniques dont work for me, wont solve my problem. These methods will tell you what the file path is of the script that is running it. It is also possible to have one script initiate another with the include() command... which still works according to your techniques. The URL that is returned will be that of the highest level, the first script that initiated the cascade of scripts.

    But, HTML is capable of referencing PHP, say, in an , , or tag or something (like in the case of GD Image Libraries). The above techniques will not work for this situation. The above techniques will only return the URL of the highest level PHP script file, not the URL in the address bar, the URL of the page being displayed, the page that initiated the script in the first place.

    Is there a way to read directly the URL that the browser is displaying? This would be the highest level URL in any given window.


  • I have been looking for this for quite some time, thanks


  • Note that on Windows server's using IIS as the Web server, this line will NOT work as desired now with PHP 5 (and maybe earlier versions):

    $http = ($_SERVER['HTTPS'] ? 'https://' : 'http://');

    On IIS a string of 'off' is returned for the evaluation of $_SERVER['HTTPS'] for non-https sessions (e.g. http://).

    Thus, I used:
    $http = (strtoupper($_SERVER['HTTPS']) == 'OFF' ? 'http://' : 'https://');

    Also, in the original post's code example, note that there will be a difference between $url and $url2 when the request does NOT contain a query string. Specifically, $url will ALWAYS contain a trailing question mark (due to the string concatenation) whereas $url2 will only have the question mark when one was actually contained in the requested url. If that matters to you, you may want to test for the length of the string returned by $_SERVER['QUERY_STRING'] as it will be an empty string when no querystring parameters were supplied but > 0 when some characters followed the questionmark in the request URL.
    e.g.
    if (mb_strlen($_SERVER['QUERY_STRING']) < 1)
    {//do something to deal with a false "?" in $url}


  • I was trying to get the current url without the anchor, and this came in handy. Thanks.

Leave a Reply