Is the HTTP_HOST server variable always defined? Is the HTTP_HOST server variable always defined? apache apache

Is the HTTP_HOST server variable always defined?


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.


It is not always defined.

As quoted above, it is only defined if there is a http request. If you are running the php script from CLI (e.g php filename.php) the HTTP_HOST key will not be set.

Moreover, you should note that HTTP_HOST is defined by the client, so it is pretty much easy to fake it and it is not reliable. You should rather rely on something like SERVER_NAME.

If you are using PHP >= 5.3.0 then you should use

gethostname();

You may check here for the documentation.

If you are using PHP >= 4.2.0 & PHP < 5.3.0 then

php_uname('n');

will do the same work.