# Helper function function safeAccess(array &$myarray, string $key): string { if (!isset($myarray[$key])) return ''; return $myarray[$key]; } function getMyUrl(): string { $scheme = getScheme(); # See other page $host = safeAccess($_SERVER, 'HTTP_HOST'); $uri = safeAccess($_SERVER, 'REQUEST_URI'); return "$scheme://$host$uri"; } # If you want the URL without the query string function getMyUrlNoQs(): string { $url = getMyUrl(); return substr($url, 0, strrpos($url, '?')); }
Programming Tips - PHP: get my URL in php
Date: 2016nov26
Update: 2025jan9
Language: php
Q. PHP: get my URL in php
A. Glue together some environment variables like this: