Home  >  Article  >  Backend Development  >  How Can I Get the URL of the Current Page in PHP?

How Can I Get the URL of the Current Page in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-01 10:42:02717browse

How Can I Get the URL of the Current Page in PHP?

Get the URL of the Current Page in PHP

In PHP, accessing the URL of the current page is a simple task. This knowledge is particularly useful for tasks such as redirecting users or generating links.

Solution

The $_SERVER superglobal array contains a wealth of information about the current request, including the URL. To retrieve the URL, you can use the following code:

<code class="php">$url = $_SERVER['REQUEST_URI'];</code>

This variable contains the entire URL, including the domain and protocol. If you only need the parts after the domain (e.g., "/example/page.php"), you can use the following:

<code class="php">$url = $_SERVER['REQUEST_URI'];
$parts = explode($GLOBALS['HTTP_HOST'], $url);
$url = $parts[1];</code>

Query String

If you also require the query string (the portion of the URL that appears after the "?" symbol), you can access it using:

<code class="php">$queryString = $_SERVER['QUERY_STRING'];</code>

The above is the detailed content of How Can I Get 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