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:

CODE:
  1. <?php
  2.   // find out the domain:
  3.   $domain = $_SERVER['HTTP_HOST'];
  4.   // find out the path to the current file:
  5.   $path = $_SERVER['SCRIPT_NAME'];
  6.   // find out the QueryString:
  7.   $queryString = $_SERVER['QUERY_STRING'];
  8.   // put it all together:
  9.   $url = "http://" . $domain . $path . "?" . $queryString;
  10.   echo "The current URL is: " . $url . "<br />";
  11.  
  12.   // An alternative way is to use REQUEST_URI instead of both
  13.   // SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
  14.   $url2 = "http://" . $domain . $_SERVER['REQUEST_URI'];
  15.   echo "The alternative way: " . $url2;
  16. ?>

If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


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

  1. Gravatar Icon 1 Zudolou

    You might want to get the https as well.

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

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

  2. Gravatar Icon 2 Mr. Brown Eyes

    How about anchor link? #anchor

  3. Gravatar Icon 3 Lalit

    thanks dear,
    very very thanking you.

  4. Gravatar Icon 4 Nick

    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.

  5. Gravatar Icon 5 Kenneth

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

Leave a Reply

You must log in to post a comment.


Categories