Getting hostname or server IP address in PHP

I need to create an anchor that will navigate to a different port on the server. For example, our server is located on domain.com:555 , we need a link to go to domain.com:777 . The trick is that it is not always domain.com. We can expect DNS to work, in which case we will use the ip address for navigation, like xx.xx.xx.xx:555 and xx.xx.xx.xx:777 .

I need to get the hostname of the server that the PHP script is running on. I tried using SERVER_ADDR, but for this reason I was given a private IP address of the server.

So how do I get the domain / ip part of the url?

+4
source share
2 answers

Do you want to:

 $_SERVER['HTTP_HOST'] 

From $_SERVER predefined variable that for 'HTTP_HOST' contains:

The contents of the Host: header from the current request, if any.

You can also find:

 $_SERVER['SERVER_NAME'] 

useful, and this discussion is relative to their relative merits.

+14
source

Try $_SERVER['HTTP_HOST']; this will give you the hostname / domain name

+3
source

All Articles