How to Extract the URL Path From the Current Page in PHP?

Barbara Streisand
Release: 2024-11-02 00:31:30
Original
203 people have browsed it

How to Extract the URL Path From the Current Page in PHP?

Retrieve URL of Current Page in PHP

Question: Determine the URL of the current page using PHP, excluding the "http://domain.example" portion.

Answer:

To retrieve the URL of the current page in PHP, utilize the following methods:

  • $_SERVER['REQUEST_URI']: Stores the complete URL path (everything after "http://domain.example").
  • $_SERVER['QUERY_STRING']: Captures the query string (if present), which appears after the "?" in the URL.

Example:

<code class="php">// Get the URL path
$urlPath = $_SERVER['REQUEST_URI'];

// Get the query string (optional)
if (isset($_SERVER['QUERY_STRING'])) {
    $queryString = $_SERVER['QUERY_STRING'];
}</code>
Copy after login

Additional Notes:

The $_SERVER array provides access to various information about the current request, including the URL and other server variables. For more details, refer to the PHP manual page for $_SERVER.

The above is the detailed content of How to Extract the URL Path From 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!