HOWTO get the real host name of your server in PHP

In PHP, to get the name of the web server, you can print $_SERVER[“HTTP_HOST”]. However, if you use round-robin DNS or a load balancer, this will give you the name Apache thinks your machine is.

What if you want the real hostname — what you’d get from hostname or uname?
print gethostbyaddr("127.0.0.1");

That’s all. It sure beats the pants off:
print trim(`hostname`);

keywords: php hostname local computer server HTTP_HOST

One Response to HOWTO get the real host name of your server in PHP

  1. Jack says:

    That sounds interesting. Found your blog through Google, you got some good rankings!

Leave a Reply

Your email address will not be published. Required fields are marked *