I tried to get my ip address a dozen different ways from within php (no shell_exec's) but couldn't make it happen, so I wrote something to do this for me. Trial and error told me which results to return. The linux case was tested on XUbuntu; I'm guessing it's going to be the same (substr($matches[11], 5)) on Ubuntu. When I need my remote ip in a cli application I use the following function:
function getServerAddress() { // returns active ip
global $verbose;
if ($verbose) print "checking for Operating System...\n";
switch(PHP_OS) {
case "Darwin": if ($verbose) print "Mac OS X found\n";
$ifconfig = shell_exec('ifconfig | grep netmask | grep broadcast');
$matches = explode(" ", $ifconfig);
//print_r($matches);
return $matches[1];
//break;
case "Linux": if ($verbose) print "Linux found\n";
$ifconfig = shell_exec('ifconfig | grep Bcast');
$matches = explode(" ", $ifconfig);
//print_r($matches);
return substr($matches[11],5);
//break;
default: if ($verbose) die("Could not determine Operating System!");
}
}
Call it with something like this:
$myIP = getServerAddress();
This function is really useful when developing on a laptop, usually connected to a lan with a dhcp-assigned address.
When I feel the urge to add windows support it will go in as another case, but I really haven't used windows much in the past year.