Skip to content

Instantly share code, notes, and snippets.

@shierw
Created August 17, 2017 03:13
Show Gist options
  • Save shierw/688e76a22475888b0d5af0f594b62b51 to your computer and use it in GitHub Desktop.
Save shierw/688e76a22475888b0d5af0f594b62b51 to your computer and use it in GitHub Desktop.
/**
* 获取当前 url
* @param $use_forwarded_host
* @return string
*/
function getCurrentUrl($use_forwarded_host = false) {
$s = $_SERVER;
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on');
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((! $ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = ($use_forwarded_host && isset($s['HTTP_X_FORWARDED_HOST'])) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
$urlOrigin = $protocol . '://' . $host;
return $urlOrigin.$s['REQUEST_URI'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment