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...
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With