醉菜鸟 2021-11-26 09:00:41

php获取当前域名

/**
 * 当前是否ssl
 * * @return bool
 */
function is_ssl(){
    if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
        return true;
    } elseif (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && 'on' == strtolower($_SERVER['HTTP_FRONT_END_HTTPS'])) {        return true;
    } elseif (isset($_SERVER['HTTP_X_CLIENT_SCHEME']) && 'https' == $_SERVER['HTTP_X_CLIENT_SCHEME']){
        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;
}
/**
 * 获取当前包含协议、端口的域名
 * @access public * @param
 * bool
 * $port 是否需要去除端口号 * @return string
 */
function domain($port = false){
    $host = $_SERVER['HTTP_X_REAL_HOST'] ?: $_SERVER['HTTP_HOST'];
    if($port){
        $host = strpos($host, ':') ? strstr($host, ':', true) :$host;
    }
    return is_ssl() ? 'https' : 'http' . '://' . $host;
}
意见反馈