# Find the current URL in 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:

×

## Subscribe

The latest posts sent straight to your inbox.