What is the difference between HTTP_HOST
and SERVER_NAME
in PHP?
where:
HTTP_HOST
=== $_SERVER[\'HTTP_HOST\'
$_SERVER['SERVER_NAME'] is based on your web servers configuration. $_SERVER['HTTP_HOST'] is based on the request from the client.
If you want to check through a server.php or whatever, you want to call it with the following:
<?php
phpinfo(INFO_VARIABLES);
?>
or
<?php
header("Content-type: text/plain");
print_r($_SERVER);
?>
Then access it with all the valid URLs for your site and check out the difference.
It took me a while to understand what people meant by 'SERVER_NAME
is more reliable'. I use a shared server and does not have access to virtual host directives. So, I use mod_rewrite in .htaccess
to map different HTTP_HOST
s to different directories. In that case, it is HTTP_HOST
that is meaningful.
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.
However, the situation is different with IP-based virtual hosts. In this case and only in this case, SERVER_NAME
and HTTP_HOST
can be different, because now the client selects the server by the IP, not by the name. Indeed, there might be special configurations where this is important.
So, starting from now, I will use SERVER_NAME
, just in case my code is ported in these special configurations.
Depends what I want to find out. SERVER_NAME is the host name of the server, whilst HTTP_HOST is the virtual host that the client connected to.
As balusC said SERVER_NAME is not reliable and can be changed in apache config , server name config of server and firewall that can be between you and server.
Following function always return real host (user typed host) without port and it's almost reliable:
function getRealHost(){
list($realHost,)=explode(':',$_SERVER['HTTP_HOST']);
return $realHost;
}
As I mentioned in this answer, if the server runs on a port other than 80 (as might be common on a development/intranet machine) then HTTP_HOST
contains the port, while SERVER_NAME
does not.
$_SERVER['HTTP_HOST'] == 'localhost:8080'
$_SERVER['SERVER_NAME'] == 'localhost'
(At least that's what I've noticed in Apache port-based virtualhosts)
Note that HTTP_HOST
does not contain :443
when running on HTTPS (unless you're running on a non-standard port, which I haven't tested).
As others have noted, the two also differ when using IPv6:
$_SERVER['HTTP_HOST'] == '[::1]'
$_SERVER['SERVER_NAME'] == '::1'