You can use $_SERVER['REQUEST_URI']
to get requested path. Then, you'll need to remove the parameters...
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
Then, add in the hostname and protocol.
echo 'http://' . $_SERVER['HTTP_HOST'] . $uri_parts[0];
You'll have to detect protocol as well, if you mix http:
and https://
. That I leave as an exercise for you. $_SERVER['REQUEST_SCHEME']
returns the protocol.
Putting it all together:
echo $_SERVER['REQUEST_SCHEME'] .'://'. $_SERVER['HTTP_HOST']
. explode('?', $_SERVER['REQUEST_URI'], 2)[0];
...returns, for example:
http://example.com/directory/file.php
php.com Documentation:
- $_SERVER — Server and execution environment information
- explode — Split a string by a string
- parse_url — Parse a URL and return its components (possibly a better solution)