PHP: HTTP_HOST vs. SERVER_NAME

software development

#1

Philosphically speaking, I’m looking for some guru-thoughts on the better of [color=#CC0000]_SERVER[“HTTP_HOST”][/color] or [color=#CC0000]_SERVER[“SERVER_NAME”][/color] to use when building things like a “page-stamp” in a footer, i.e., showing the user the URL of the page they’re actually looking at, on the off-chance that my site gets “framed” in some way (for an example of what I’m talking about, see a page at my site, e.g. http://genes.gilkison.net/ky10cav/dyer.html).

I realize that they both display the same thing at this point (at least they do on my DH setup), but what is the difference supposed to be? I try to build my scripts, such as they are, defensively, and these things seem to change occasionally from one version of PHP to another, so I’d like ot try and use the appropriate one…


#2

This just came up on a mailing list I subscribe to. The person posting on that list recommended using HTTP_HOST, and falling back on SERVER_NAME only if HTTP_HOST was not set. He said that SERVER_NAME could be unreliable on the server for a variety of reasons, including:

  • no DNS support
  • misconfigured
  • behind load balancing software

The original post was in the context of the source code for an application that other list subscribers would be installing on a wide variety of servers. I’m not sure how this relates to your situation, but I thought I would pass on the info in case you might find it useful.


#3

Do you have a link to the post on the difference between using HTTP_HOST and SERVER_NAME please?


#4

HTTP_HOST is the value sent by the client; it matches what appears in the address bar. SERVER_NAME is the “canonical” name of your domain as configured on the web server.

In most cases, these should be the same. Situations where they can differ include:

[list]
[] www vs. non-www, if your domain isn’t configured to redirect one to the other
[
] direct access to a site using its unique IP
[*] wildcard domains, if configured
[/list]

By the way, please note that the post you’re replying to here is over 13 years old. Chances are that the mailing list being referenced no longer exists.