views:

71

answers:

4

All,

I am using the following command to retrieve the domain name of my server.

$_SERVER['HTTP_HOST']

This seems to return the IP address instead of domain name like www.example.com. I looked at PHPInfo and it also lists an IP address for HTTP_HOST instead of Domain name. What do I need to change to make the domain name appear instead of IP?

Thanks

+5  A: 

Use $_SERVER['SERVER_NAME'] instead.

Or, you can look at every Server Variable you have available but putting this script in one of your PHP pages on this server.

<?PHP
foreach($_SERVER as $key_name => $key_value) {
  print $key_name . " = " . $key_value . "<br>";
}
?>
Shawn Steward
That also lists IP address instead of domain name
Vincent
@Vincent: If `$_SERVER['SERVER_NAME']` lists an IP address, Apache is misconfigured, as this should contain the value of Apache's ServerName directive: http://httpd.apache.org/docs/2.2/mod/core.html#servername
R. Bemrose
+1  A: 

$_SERVER['HTTP_HOST'] (which may not be defined if the client made a HTTP/1.0 request) contains the hostname that the client requested.

If the client requested http://127.0.0.1/ it would contain 127.0.0.1; for http://localhost/ it would contain localhost; for http://127.0.0.1:81/ it would contain 127.0.0.1:81.

Artefacto
Is it okay to define the IP address with a host name in /etc/hosts file and probably this $_SERVER["SERVER_NAME"] or $_SERVER["HTTP_HOST"] will pick it up?
Vincent
A: 

useful link: http://php.net/manual/en/reserved.variables.server.php use $_SERVER["SERVER_NAME"]

VoodooChild
+1  A: 

gethostbyaddr() can retrieve a hostname for the IP address, but only if DNS record has been set up properly

Col. Shrapnel