How to get the current page address
Short answer
The path with parameters is in $_SERVER['REQUEST_URI']:
<?php
echo $_SERVER['REQUEST_URI'];The full address
Build it from the scheme, the host and the path:
<?php
$scheme = ($_SERVER['HTTPS'] ?? 'off') === 'on' ? 'https' : 'http';
$url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];The path only
parse_url() drops the parameters after the question mark:
<?php
echo parse_url('/catalog/tea?page=2&sort=price', PHP_URL_PATH);Output:
/catalog/teaFunctions used in this recipe
- parse_url — Splits a URL into its parts.
- http_build_query — Builds a URL query string from an array.
- htmlspecialchars — Replaces HTML special characters with safe entities.