Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Domain Name instead of IP

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

like image 919
Jake Avatar asked May 21 '10 16:05

Jake


2 Answers

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>";
}
?>
like image 98
Shawn Steward Avatar answered Nov 03 '22 07:11

Shawn Steward


$_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.

like image 40
Artefacto Avatar answered Nov 03 '22 07:11

Artefacto