Home >Backend Development >PHP Tutorial >How Do I Fetch the URL of the Current Page in PHP?

How Do I Fetch the URL of the Current Page in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-30 22:35:30340browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn