【问题标题】:PHP getting full server name including port number and protocolPHP获取完整的服务器名称,包括端口号和协议
【发布时间】:2011-09-15 13:04:15
【问题描述】:

在 PHP 中,有没有一种可靠的好方法来获得这些东西:

协议:即 http 或 https 服务器名称:例如本地主机 端口号:例如8080

我可以使用$_SERVER['SERVER_NAME'] 获取服务器名称。

我可以得到协议,但我不认为它是完美的:

    if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https') {
        return "https";
    }
    else {
        return "http";
    }

我不知道如何获取端口号。我使用的端口号不是 80.. 它们是 8080 和 8888。

谢谢。

【问题讨论】:

  • print_r($_SERVER) 看看它给了你什么。

标签: php


【解决方案1】:

看看documentation

我想你想要$_SERVER['SERVER_PORT']

【讨论】:

  • if($_SERVER['SERVER_PORT'] != '443') { $URL = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];标头(“位置:$ URL”); ?> }
【解决方案2】:

$_SERVER['SERVER_PORT'] 将为您提供当前使用的端口。

【讨论】:

    【解决方案3】:

    这是我使用的:

        function my_server_url()
        {
            $server_name = $_SERVER['SERVER_NAME'];
    
            if (!in_array($_SERVER['SERVER_PORT'], [80, 443])) {
                $port = ":$_SERVER[SERVER_PORT]";
            } else {
                $port = '';
            }
    
            if (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) {
                $scheme = 'https';
            } else {
                $scheme = 'http';
            }
            return $scheme.'://'.$server_name.$port;
        }
    

    【讨论】:

      【解决方案4】:

      从上面编译:

      function getMyUrl()
      {
        $protocol = (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) ? 'https://' : 'http://';
        $server = $_SERVER['SERVER_NAME'];
        $port = $_SERVER['SERVER_PORT'] ? ':'.$_SERVER['SERVER_PORT'] : '';
        return $protocol.$server.$port;
      }
      

      【讨论】:

        【解决方案5】:
        <?php
        
        $services = array('http', 'ftp', 'ssh', 'telnet', 'imap', 'smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www');
        
        foreach ($services as $service) {
            $port = getservbyname($service, 'tcp');
            echo $service . ":- " . $port . "<br />\n";
        }
        
        ?>
        

        这是显示所有端口号。

        如果你已经知道端口号,你可以这样做,

        echo  getservbyport(3306, "http");   // 80
        

        【讨论】:

          【解决方案6】:
          $protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
          $hostname = $_SERVER['SERVER_ADDR'];
          $port = $_SERVER['SERVER_PORT'];
          

          【讨论】:

            【解决方案7】:
             if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,4))=='http') {
                    $strOut = sprintf('http://%s:%d', 
                                   $_SERVER['SERVER_ADDR'],
                                   $_SERVER['SERVER_PORT']);
                } else {
                     $strOut = sprintf('https://%s:%d', 
                                   $_SERVER['SERVER_ADDR'],
                                   $_SERVER['SERVER_PORT']);
                }
            
             return $strOut;
            

            如果你想尝试类似的东西

            【讨论】:

              【解决方案8】:

              服务器端没有任何效果,APACHE 出了点问题,我无法访问 服务器,我最终通过 Javascript 重定向到 http,这不是理想的解决方案,也许这可以在我的情况下拯救其他人

              <script>
              if(!window.location.href.startsWith('https')) 
                  window.location.href = window.location.href.replace('http','https');
              </script>
              

              【讨论】:

                【解决方案9】:

                你为什么不得到这样的完整网址

                strtolower(array_shift(explode("/",$_SERVER['SERVER_PROTOCOL'])))."://".$_SERVER['SERVER_NAME'];
                

                或者(如果你想要来自 HTTP 的主机名)

                strtolower(array_shift(explode("/",$_SERVER['SERVER_PROTOCOL'])))."://".$_SERVER['HTTP_HOST'];
                

                【讨论】:

                • this,如果你想使用array_shift(),这是关于严格的标准。
                猜你喜欢
                • 2017-09-10
                • 1970-01-01
                • 2012-06-22
                • 2011-07-13
                • 1970-01-01
                • 1970-01-01
                • 2016-08-25
                • 1970-01-01
                • 2012-09-28
                相关资源
                最近更新 更多