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

Linda Hamilton
Release: 2024-10-30 22:35:30
Original
293 people have browsed it

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

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!

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