$_SERVER['HTTP_HOST']和$_SERVER['SERVER_NAME']在PHP中的区别是什么?

什么时候你会考虑使用其中一种而不是另一种,为什么?


当前回答

正如balusC所说,SERVER_NAME是不可靠的,可以在apache配置中更改,服务器的服务器名配置以及您和服务器之间的防火墙。

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

function getRealHost(){
   list($realHost,)=explode(':',$_SERVER['HTTP_HOST']);
   return $realHost;
}

其他回答

我花了一段时间才理解人们所说的“SERVER_NAME更可靠”是什么意思。我使用共享服务器,不能访问虚拟主机指令。因此,我在.htaccess中使用mod_rewrite来将不同的HTTP_HOSTs映射到不同的目录。在这种情况下,HTTP_HOST是有意义的。

The situation is similar if one uses name-based virtual hosts: the ServerName directive within a virtual host simply says which hostname will be mapped to this virtual host. The bottom line is that, in both cases, the hostname provided by the client during the request (HTTP_HOST), must be matched with a name within the server, which is itself mapped to a directory. Whether the mapping is done with virtual host directives or with htaccess mod_rewrite rules is secondary here. In these cases, HTTP_HOST will be the same as SERVER_NAME. I am glad that Apache is configured that way.

但是,基于ip的虚拟主机的情况有所不同。在这种情况下,且仅在这种情况下,SERVER_NAME和HTTP_HOST可以是不同的,因为现在客户端通过IP而不是名称来选择服务器。确实,在某些特殊的构型中这很重要。

因此,从现在开始,我将使用SERVER_NAME,以防我的代码移植到这些特殊配置中。

请注意,如果你想使用IPv6,你可能想使用HTTP_HOST而不是SERVER_NAME。如果输入http://[::1]/,则环境变量如下:

HTTP_HOST = [::1]
SERVER_NAME = ::1

这意味着,如果你做一个mod_rewrite,你可能会得到一个糟糕的结果。SSL重定向示例:

# SERVER_NAME will NOT work - Redirection to https://::1/
RewriteRule .* https://%{SERVER_NAME}/

# HTTP_HOST will work - Redirection to https://[::1]/
RewriteRule .* https://%{HTTP_HOST}/

这只适用于在没有主机名的情况下访问服务器。

看我想知道什么了。SERVER_NAME是服务器的主机名,而HTTP_HOST是客户端连接到的虚拟主机。

如果你想检查server.php或其他什么,你想用下面的方法调用它:

<?php
    phpinfo(INFO_VARIABLES);
?>

or

<?php
    header("Content-type: text/plain");

    print_r($_SERVER);
?>

然后使用您站点的所有有效url访问它,并检查差异。

$_SERVER['SERVER_NAME']基于您的web服务器配置。 $_SERVER['HTTP_HOST']是基于来自客户端的请求。