Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simply find the ip address of server

The following code to find the actual local ip address (e.g. 192.168.1.X) of the host server has run fine on a dev machine

$ip = getHostByName( getHostName() );

Transferring it to another server it started to output 127.0.0.1

It seems that getHostName() returns localhost which in turn gets converted to 127.0.0.1 by getHostByName().

Looking at other questions here it seemed that $_SERVER['SERVER_ADDR'] might solve the puzzle but this is also returning 127.0.0.1

Checking and searching the output from phpinfo() returns no clues to the actual ip address of the machine.

Seriously, does anyone know a bullet proof (simpler the better) way of getting the ip address of the machine running the script? Why is it so hard to get such fundamental data without doing back flips?

I really don't want to be hardcoding the ip address in a config file...

like image 395
zaf Avatar asked Jan 03 '12 13:01

zaf


1 Answers

I'm afraid in general it's not possible - the real IP used to connect the server might be translated before it gets to the server...

like image 69
Tomas Avatar answered Oct 10 '22 18:10

Tomas