How to Read or Get the Full URL Path of the Current Webpage in PHP?


You’re trying to read the URL of the current web page you’re visiting.


At first, let’s see different parts of a URL. A URL may have the following parts-

Parts of an url

Protocol: http or https is the protocol. In the above picture, https is the protocol.

Domain: After://, domain starts and end till the next forward slash(/). The domain part may include sub-domain. In the above image, is the domain.

Path: After domain, the path refers to the path of the script you’re currently on.

PHP stores URL information in $_SERVER[] array

Web server stores URL path of a webpage you visit along with other information in the $_SERVER[] array. Each part of the URL that you see in the above picture is saved in each different index in the $_SERVER[] array. $_SERVER is an associative array.

Retrieve URL path information

To retrieve different parts of an URL, use different indices in the $_SERVER[] array.

The following picture shows parts of an URL and $_SERVER[] arrays with different keys to retrieve those URL parts. Compare matching the colors.

Parts of an url & PHP code to retrieve those

Let’s see the PHP code to read full path of the current web page URL-

echo isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

[wpdm_file id=50]