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!