美文网首页
获取端口和域名

获取端口和域名

作者: 知码客 | 来源:发表于2022-06-09 11:20 被阅读0次
    /**
     * 功能 : 获取域名
     * @return 
     */
    function serverName() {
        if(isset($_SERVER['HTTP_HOST'])){
            if(stripos($_SERVER['HTTP_HOST'],']')) {
                $thisserver_names=explode(']',$_SERVER['HTTP_HOST']);
                $name=$thisserver_names[0].']';
            }else {
                $thisserver_names=explode(':',$_SERVER['HTTP_HOST']);
                $name=$thisserver_names[0];
            }
        }else {
            if(!isset($_SERVER['SERVER_NAME'])) {$_SERVER['SERVER_NAME']='';}
            $name=$_SERVER['SERVER_NAME'];
        }
        Return strtolower($name);
    }
    
    /**
     * 功能 : 获取端口号
     * @return 
     */
    function serverPort($colon=true) {
        if(isset($_SERVER['HTTP_HOST'])){
            if(stripos($_SERVER['HTTP_HOST'],']')) {
                $thisserver_port=explode(']:',$_SERVER['HTTP_HOST']);
            }else {
                $thisserver_port=explode(':',$_SERVER['HTTP_HOST']);
            }
            if(isset($thisserver_port[1]) && is_numeric($thisserver_port[1])) {
                $port=$thisserver_port[1];
            }else {
                $port='80';
            }
        }elseif(isset($_SERVER['SERVER_PORT']) && is_numeric($_SERVER['SERVER_PORT'])) {
            $port=$_SERVER['SERVER_PORT'];
        }else {
            $port='80';
        }
        if($port=='80') {
            Return '';
        }elseif($colon) {
            Return ':'.$port;
        }else{
            Return $port;
        }
    }
    

    相关文章

      网友评论

          本文标题:获取端口和域名

          本文链接:https://www.haomeiwen.com/subject/ppcmmrtx.html