Here is my solution - code is inspired by Tracy Debugger. It was changed for support different server ports. You can get full current URL including $_SERVER['REQUEST_URI']
or just the basic server URL. Check my function:
function getCurrentUrl($full = true) {
if (isset($_SERVER['REQUEST_URI'])) {
$parse = parse_url(
(isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') .
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) . (($full) ? $_SERVER['REQUEST_URI'] : null)
);
$parse['port'] = $_SERVER["SERVER_PORT"]; // Setup protocol for sure (80 is default)
return http_build_url('', $parse);
}
}
Here is test code:
// Follow $_SERVER variables was set only for test
$_SERVER['HTTPS'] = 'off'; // on
$_SERVER['SERVER_PORT'] = '9999'; // Setup
$_SERVER['HTTP_HOST'] = 'some.crazy.server.5.name:8088'; // Port is optional there
$_SERVER['REQUEST_URI'] = '/150/tail/single/normal?get=param';
echo getCurrentUrl();
// http://some.crazy.server.5.name:9999/150/tail/single/normal?get=param
echo getCurrentUrl(false);
// http://some.crazy.server.5.name:9999/