The default external IP address for the local machine can be obtained as follows:
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_connect($sock, "8.8.8.8", 53); socket_getsockname($sock, $name); // $name passed by reference $localAddr = $name;
How does it work:
Since this is UDP, socket_connect does not go online. It only assigns the local address and port to the socket. This allows us to get the local address.
This solution works without network traffic, DNS, or command execution.
May not work if there is no route to 8.8.8.8 (for example, you do not have an Internet connection).
Arnaud Le Blanc Apr 13 '16 at 16:31 2016-04-13 16:31
source share