23

Possible Duplicate:
HTTP_HOST vs. SERVER_NAME

What is the difference between $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] ??

Community
  • 1
  • 1
Eman
  • 333
  • 1
  • 2
  • 7

3 Answers3

48

$_SERVER['SERVER_NAME'] gives the value of the server name as defined in host configuration (i.e for Apache the Apache .conf file).

$_SERVER['HTTP_HOST'] gives you the domain name through which the current request is being fulfilled and is more directly related to the request.

HTTP_HOST is typically more useful in most applications in that it relates directly to the request, whereas SERVER_NAME could return whatever value is in the conf file and doesn't tell you anything about the request at all.

I will give you an example of how HTTP_HOST might differ from SERVER_NAME. Say you have an host defined in Apache with ServerName of example.com and an IP address of 1.2.3.4.

Let's look at two incoming request URLs and show the difference between these variables:

http://www.example.com
HTTP_HOST = www.example.com
SERVER_NAME = example.com

http://1.2.3.4
HTTP_HOST = 1.2.3.4
SERVER_NAME = example.com

So again, HTTP_HOST is tied more to the request, whereas SERVER_NAME is determined by server configuration.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • $_SERVER['HTTP_HOST'] "gives you the domain name through which the current request is being fulfilled and it more related to the request." +1 THANKS!!!! And Thanks to people linking to the official doc, but too complicated for me ;) – Eman Dec 08 '12 at 01:10
  • @Mike, It's not just `SERVER_NAME` that depends on the server configuration. [`HTTP_HOST` also depends on it.](http://stackoverflow.com/a/28889208/632951) – Pacerier Mar 05 '15 at 23:50
2

HTTP_HOST is the Host: header sent by the client. As a result, it might be a little less trustworthy. SERVER_NAME is determined by your server's configuration, regardless of user input.

The difference in behavior is subtle. Some good examples are demonstrated here: http://shiflett.org/blog/2006/mar/server-name-versus-http-host

The docs explain this well

'SERVER_NAME' The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

'HTTP_HOST' Contents of the Host: header from the current request, if there is one.

Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
  • Your conclusion is wrong. `SERVER_NAME` is determined by your server's configuration, and [can be made (indeed *that's* the default behavior) to depend](http://stackoverflow.com/a/28889208/632951) on user input. – Pacerier Mar 05 '15 at 23:51
  • A good example of the subtleties of HTTP_HOST versus SERVER_NAME is demonstrated here: http://shiflett.org/blog/2006/mar/server-name-versus-http-host – Frank Farmer Mar 07 '15 at 00:10
  • Yes it's the same one from http://stackoverflow.com/a/1459794/632951 . `SERVER_NAME` is determined by server's config, and can be made to depend on user input. – Pacerier Mar 08 '15 at 13:21
  • @Pacerier I think it's accurate to say it's the default behaviour of Apache. The source doesn't say anything other than Apache. – 張俊芝 Mar 04 '21 at 03:59
0

HTTP_HOST

Contents of the Host: header from the current request, if there is one.

SERVER_NAME

The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

http://php.net/manual/en/reserved.variables.server.php

mpen
  • 272,448
  • 266
  • 850
  • 1,236