PHP得到局域网IP的函数,还不是很好用,哪位大神帮忙优化一下

来源:互联网 发布:银湖网骗局 知乎 编辑:程序博客网 时间:2024/05/01 20:38

我写的原代码如下:

function the_lan_ip() {//

if (!is_windows_os()) { //如果不是windows操作系统,执行ifconfig,去捉相应的IPv4的IP
exec('ifconfig', $output);
for ($i = 0; $i <= count($output); $i++) {
error_reporting(0);
preg_match('/\inet.*:(.*)\040/', $output[$i], $the_ip_str); //通过正则去找inet相对应的IP地址就是局域网IP了
if (count($the_ip_str) > 0) {
$the_lan_ip[$i] = $the_ip_str[1];
}
}
} else {exec('ipconfig', $output); //如果是windows操作系统,执行ipconfig,去捉相应的IPv4的IP
for ($i = 0; $i <= count($output); $i++) {
error_reporting(0);
preg_match('/\IPv4.*:(.*)/', $output[$i], $the_ip_str); //通过正则去找IPv4相对应的IP地址就是局域网IP了
if (count($the_ip_str) > 0) {
$the_lan_ip[$i] = $the_ip_str[1];
}
}
}
return ($the_lan_ip);
}
0 0
原创粉丝点击