Is the HTTP_HOST server variable always defined?

Is the HTTP_HOST server HTTP_HOST always defined for all servers, or, for example, in IIS is defined with a different name or not even defined at all?

Also, is this value always defined? Or some host they do not define this value? Is there any other way to get this value?

+7
source share
3 answers

Guideline Note

HTTP_HOST -

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

HTTP_HOST is part of the client’s HTTP request and indicates which hostname the request should be directed to. if HTTP_HOST is not set, the client is either very, very old (HTTP 1.0 does not support HTTP_HOST), or made a request directly to your IP address of your website.

I think Host HTTP header is required as HTTP 1.1

+7
source

HTTP_HOST not defined by the server, it

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

Thus, it depends on whether the header information of your request contains Host .

+2
source

This is not always determined.

As indicated above, it is determined only if there is an HTTP request. If you use php script from the CLI (e.g. php filename.php), the HTTP_HOST key HTTP_HOST not be set.

In addition, you should notice that HTTP_HOST is defined by the client, so it is fairly easy to fake, and it is not reliable. You should rather rely on something like SERVER_NAME .

If you are using PHP> = 5.3.0 you should use

 gethostname(); 

You can check here for documentation.

If you are using PHP> = 4.2.0 and PHP <5.3.0, then

 php_uname('n'); 

will do the same job.

+2
source

All Articles