PHP 获取完整的服务器名称,包括端口号和协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7431313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:16  来源:igfitidea点击:

PHP getting full server name including port number and protocol

php

提问by ale

In PHP, is there a reliableand good way of getting these things:

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

Protocol: i.e. http or https Servername: e.g. localhost Portnumber: e.g. 8080

协议:即 http 或 https 服务器名称:例如 localhost 端口号:例如 8080

I can get the server name using $_SERVER['SERVER_NAME'].

我可以使用$_SERVER['SERVER_NAME'].

I can kind of get the protocol but I don't think it's perfect:

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

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

I don't know how to get the port number though. The port numbers I am using are not 80.. they are 8080 and 8888.

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

Thank you.

谢谢你。

回答by Jonnix

Have a look at the documentation.

查看文档

You want $_SERVER['SERVER_PORT']I think.

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

回答by Narf

$_SERVER['SERVER_PORT']will give you the port currently used.

$_SERVER['SERVER_PORT']会给你当前使用的端口。

回答by lfjeff

Here's what I use:

这是我使用的:

    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;
    }

回答by Sumith Harshan

<?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";
}

?>

This is display all port numbers.

这是显示所有端口号。

If you already know port number you can do like this,

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

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

回答by sbrbot

Compiled from above:

从上面编译:

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;
}

回答by Alix Axel

$protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
$hostname = $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_PORT'];

回答by DarkMantis

 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;

Try something like that if you want

如果你愿意,试试这样的事情

回答by Jonathan dos Santos

nothing worked serverside , something was wrong on APACHE and I had no access to the server and I ended up redirecting to http throught Javascript, It's not the ideal solution maybe this can save someone else in my situation

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

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

回答by Avesh

Why don't you get full url like this

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

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

or (If you want host name from HTTP)

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

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