"php: $_server variables: $_server['http_host'] vs $_server['server_name'] [duplicate]" Answer’s

0

$_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 domain.com and an IP address of 1.2.3.4.

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

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

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

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

Wednesday, March 31, 2021
 
Dunc
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :