11

我不知道也许这是一个错误。

我的服务器上有 2 个虚拟主机。

virtualhost1.com virtualhost2.com

如果我使用端口 80 (virtualhost1.com:80) 打开 virtualhost1.com

$_SERVER['HTTP_HOST']='virtualhost1.com';

但是如果我打开 virtualhost2.com:80

$_SERVER['HTTP_HOST']='virtualhost2.com:80'; // NOTE: with port number

我能知道为什么吗?

4

2 回答 2

21

The value of $_SERVER['HTTP_HOST'] is taken directly from the Host: HTTP request header. It appears the requesting client is filling it in that way.

I suggest using $_SERVER['SERVER_NAME'] instead as its value will be set from your virtual host config. However, as Flimm notes below, even the reliability of SERVER_NAME can still be dependent on server config (check out this answer for more info on that).

于 2010-12-21T23:21:46.323 回答
2

以下函数总是返回没有端口的真实主机(用户键入的主机),它几乎是可靠的:

function getRealHost(){
   list($realHost,)=explode(':',$_SERVER['HTTP_HOST']);
   return $realHost;
}
于 2016-05-20T13:17:25.420 回答