Skip to content

Instantly share code, notes, and snippets.

@shierw
Created August 17, 2017 03:13

Revisions

  1. shierw created this gist Aug 17, 2017.
    17 changes: 17 additions & 0 deletions 获取当前 URL.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    /**
    * 获取当前 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'];
    }