The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hmm... I'm asking this because I had a situation where the SERVER_NAME was being returned as an IP address.. and the HOST_NAME was being returned as www.domain.com

I need the "domain.com", and not the IP address.. but in the past people told me I should use SERVER_NAME to do this.

So now that I know it doesn't always work, I am asking what is the most reliable method to get the hostname.

freakysid, from what you describe.. it sounds like I should use HOST_NAME.

Has anyone had any experience where there was no hostname returned when using $_SERVER['HOST_NAME']? Will this return the domain name 100% of the time? (No matter if it is name-based hosting, IP-based hosting, or whatever..)

If the client (browser) does not send a HOST header with their HTTP request, and the virtual server for the domain is name based (as most are these days), the request cannot be processed by the web server and the request will end up in the error-log and an error will be returned to the client (I don't know which number).

So, yes, I would say that using the HOST_NAME is a safe bet and is the most appropriate for your purpose.

I can't add too much because freakysid's explaination was exhaustive and complete ,
I only I add, but it was already said though, that in a HTTP request the Host field in the Header is obligatory ( it could be empty eventually ).
That's for version 1.1 of the protocol, though.
So, It's interesting to show the difference here:

>> I'm asking this because
>> I had a situation where the SERVER_NAME
>> was being returned as an IP address
SERVER_NAME,
reflects the value set inside Apache httpd.conf,
in your case (IP address returned)
I think your servername was probably set as
ServerName x.x.x.x

>> will be returned to the client
>> (I don't know which number).
400 (Bad Request),
the http specification said that.
Doing some tests I had 503,
but that's another matter...

Re: Curiosity killed the cat.

Originally posted by siteguru Why do you want to get details of the SERVER? Surely you know where your code is hosted? (I guess you realise that this method will not give you details about the CLIENT).

I have no idea where the code will be hosted.. this is why I need to somehow find out.

Thank you everyone who responded. I will use $_SERVER['HOST_NAME'] to get the correct domain name.