Is the server variable HTTP_HOST
always defined for all servers, or for example in IIS is defined with other name or even is not defined at all?
Also, that value is always defined? Or some host they don't define that value? Is there any other way to retrieve that value?
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
IIS Server Variables provide information about the server, the connection with the client, and the current request on the connection. Additionally, inbound URL rewrite rules can be used to set custom server variables.
The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config.
$host is the value of Host in request header, without the port number. If that is not present in request header, then it assumes the value of server_name directive in NGINX configuration. If there are multiple values in server_name directive, then $host will use the first value and ignore the rest.
Quoting the manual
HTTP_HOST is
Contents of the Host: header from the current request, if there is one.
HTTP_HOST is a part of the client's HTTP request and specifies which host name the request is to be directed to. if HTTP_HOST is not set, the client is either very, very old (HTTP 1.0 doesn't support HTTP_HOST) or has made a request directly to your web site's IP.
I think the Host HTTP Header is mandatory since HTTP 1.1
HTTP_HOST
is not defined by server, it's
Contents of the Host: header from the current request, if there is one.
So it depends on whether the header info of your request contain Host
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With