Home > Backend Development > PHP Tutorial > 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
Release: 2024-11-01 10:42:02
Original
838 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template