Just find the server ip address

The following code to find the actual local IP address (e.g. 192.168.1.X) of the host server works fine on the dev machine

$ip = getHostByName( getHostName() );

Transferring it to another server, he began to display 127.0.0.1

It seems that getHostName () returns localhost, which in turn is converted to 127.0.0.1via getHostByName ().

Looking at other issues, it seemed like it $_SERVER['SERVER_ADDR']could solve the puzzle, but it also brings back127.0.0.1

Checking and finding the output from phpinfo()does not return any indication of the actual IP address of the machine.

Seriously, does anyone know of a bulletproof (simpler) way to get the ip address of a machine running a script? Why is it so difficult to obtain such fundamental data without reverse flips?

I really don't want to hardcode the ip address in the configuration file ...

+5
source share
3 answers

I am afraid, in general, this is not possible - the real IP address used to connect the server may be translated before it gets to the server ...

+3
source

If you need the entire list of interfaces, try system();running the shell command and get the result on the web server. For instance:

system('ifconfig', $LIP);
system('ipconfig', $WIP);
echo "Linux:".$LIP."<br>Win:".$WIP."<br>;
+3
source

:

$ip = getHostByName( $_SERVER['HTTP_HOST'] );

IP- -, .

, http://localhost/, 127.0.0.1. http://www.mydomain.tld/ IP www.mydomain.tld.

, /etc/hosts , .

0

All Articles