Skip to content

Instantly share code, notes, and snippets.

@aurmil
Last active March 6, 2019 04:52
Show Gist options
  • Save aurmil/2449c18eb495d78268d9d910cbf9cd74 to your computer and use it in GitHub Desktop.
Save aurmil/2449c18eb495d78268d9d910cbf9cd74 to your computer and use it in GitHub Desktop.
function getBaseUrl($includeTrailingSlash = FALSE)
{
$protocol = (isset($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) ? 'https' : 'http';
$domain = php_uname('n');
$port = '';
if (isset($_SERVER['HTTP_HOST'])) {
$domain = filter_input(
INPUT_SERVER,
'HTTP_HOST',
FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES
);
} elseif (isset($_SERVER['SERVER_NAME'])) {
$domain = filter_input(
INPUT_SERVER,
'SERVER_NAME',
FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES
);
}
if (FALSE === strpos($domain, ':')
&& (('http' === $protocol && 80 !== (int) $_SERVER['SERVER_PORT'])
|| ('https' === $protocol && 443 !== (int) $_SERVER['SERVER_PORT']))
) {
$port = ':' . (int) $_SERVER['SERVER_PORT'];
}
$pathInfo = pathinfo($_SERVER['PHP_SELF']);
$baseUrl = rtrim($pathInfo['dirname'], '/');
$trailingSlash = $includeTrailingSlash ? '/' : '';
return "${protocol}://${domain}${port}${baseUrl}${trailingSlash}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment