public static getLocalIPAddress ( ) |
public static function getLocalIPAddress()
{
if (array_key_exists("SERVER_ADDR", $_SERVER) && self::isIPv4($_SERVER['SERVER_ADDR'])) {
// SERVER_ADDR is available only if we are running the CGI SAPI
return $_SERVER['SERVER_ADDR'];
} else {
if (function_exists("gethostname") && self::isIPv4(gethostbyname(gethostname()))) {
return gethostbyname(gethostname());
} else {
// fallback if nothing works
return "127.0.0.1";
}
}
}
/** * @test */ public function testGetLocalIPAddress() { $ip = $this->object->getLocalIPAddress(); $this->assertEquals($ip, filter_var($ip, FILTER_VALIDATE_IP)); $_SERVER['SERVER_ADDR'] = '127.0.0.1'; $ip = $this->object->getLocalIPAddress(); $this->assertEquals($ip, filter_var($ip, FILTER_VALIDATE_IP)); }