To get a domain name in php, we use http_host or server_name variables. But what is the difference between http_host and server_name and which one will return more accurate results?

http host can be retrived from request headers from client. As the value comes from client it can be easily modified thus its not more reliable.

In case of server name the value will be defined in server configuration, so we can think the value we get is more reliable than the previous one, but stop! what happens when the server value is not properly configured?

As per the Apache documentation, If no server name defined in config, then the server attempts to deduce the hostname from reverse lookup on the IP address.

To make server name to return more reliable results, you should have set UseCanonicalName directive to on in the  Virtual Host  entry in httpd.conf like this:
<VirtualHost *>
    UseCanonicalName on
So which one? We can say server_name more reliable than the http_host, but you should have proper server configuration.


Comments (0)
Leave a Comment

loader Posting your comment...