php 获取网站域名函数

echo getHttpHost();
/**
 * 网站是否携带openSsl证书
 *
 * @return bool
 */
function isOpenSsl()
{
     
    $server = $_SERVER;
    if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) {
     
        return true;
    }
    elseif (isset($server['REQUEST_SCHEME']) && 'https' == $server['REQUEST_SCHEME']) {
     
        return true;
    }
    elseif (isset($server['SERVER_PORT']) && ('443' == $server['SERVER_PORT'])) {
     
        return true;
    }
    elseif (isset($server['HTTP_X_FORWARDED_PROTO']) && 'https' == $server['HTTP_X_FORWARDED_PROTO']) {
     
        return true;
    }
    return false;
}

/**
 * 获取http协议类型  http|https
 *
 * @return string
 */
function getHttpScheme()
{
     
    return isOpenSsl() ? 'https' : 'http';
}


/**
 * 判断地址是否为 http开头
 *
 * @param string $url
 * @return bool
 */
function isHttp($url)
{
     
    return strpos($url, 'http') === 0;
}

/**
 * 获取http协议域名头 http://|https://
 *
 * @return string
 */
function getHttpType()
{
     
    return getHttpScheme() . '://';
}

/**
 * 获取host
 *
 * @return mixed|string
 */
function getHost()
{
     
    if (isset($_SERVER['HTTP_X_REAL_HOST'])) {
     
        $host = $_SERVER['HTTP_X_REAL_HOST'];
    }
    else {
     
        $host = $_SERVER['HTTP_HOST'];
    }
    return $host;
}

/**
 * 获取网站域名
 *
 * @param bool $http 是否返回 http 如 http://abc.com
 * @return mixed|string
 */
function getHttpHost($http = true)
{
     
    return $http ? getHttpType() . getHost() : getHost();
}

你可能感兴趣的:(php)