note 69205 deleted from function.gethostbyname by bjori
| From: | bjori@php.net | Date: | Sun, 27 Aug 2006 23:27:04 +0000 |
| Subject: | note 69205 deleted from function.gethostbyname by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-116401@lists.php.net to get a copy of this message | ||
Note Submitter: zmorris at zsculpt dot com
Reason: bad code
----
If you need something like getifaddrs() or gethostid() to get the machine's main IP address,
you can try $_SERVER['SERVER_ADDR'] but that often returns 127.0.0.1. This happens a lot
if you are just testing at home on a vanilla machine. Instead, try this:
gethostbyname( $_SERVER['SERVER_NAME'] )
It will usually return the correct IP address, something like 192.168.0.50 if you are on LAN. I
have only tested machines behind NAT, but it should work on a machine connected directly to the
internet. Perhaps check if $_SERVER['SERVER_ADDR'] is 127.0.0.1 and if so, fall back to
the above method.