您的位置:首页 > 运维架构 > Linux

php获取本地ip-针对linux

2014-01-24 14:04 267 查看
public static function getLocalIp() {
$preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
exec("ifconfig", $out, $stats);
if (!empty($out)) {
if (isset($out[1]) && strstr($out[1], 'addr:')) {
$tmpArray = explode(":", $out[1]);
$tmpIp = explode(" ", $tmpArray[1]);
if (preg_match($preg, trim($tmpIp[0]))) {
return trim($tmpIp[0]);
}
}
}
return '127.0.0.1';
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: