Home >Backend Development >PHP Tutorial >How Do I Fetch the URL of the Current Page in PHP?
Fetching the URL of the Current Page in PHP
In PHP, you can retrieve the URL of the current page by utilizing a particular variable. Let's explore how you can accomplish this.
To obtain the URL without the domain, you can employ $_SERVER['REQUEST_URI']. This variable stores the entire URL path, including the query string.
For instance:
<code class="php"><?php // Fetch the URL $url = $_SERVER['REQUEST_URI']; // Print the URL (excluding domain) echo substr($url, 7); ?></code>
In this example, the output would be the URL path, beginning after the "http://domain.example/" portion.
Additionally, if you require the query string separately, you can use the variable $_SERVER['QUERY_STRING'].
The above is the detailed content of How Do I Fetch the URL of the Current Page in PHP?. For more information, please follow other related articles on the PHP Chinese website!