PHP获取局域网ip(192.168)

有时候,程序中,需要获取本机内网ip的情况,经过各种资料查找,最终确定一下代码:

php 复制代码
//获取内网ip
    function getLocalIP() {
        exec("ipconfig /all",$arr);
        $res = mb_convert_encoding($arr, 'UTF-8', 'GBK');
        $ip = '';
        foreach ($res as $item) {
            if (strpos($item, 'DHCP 服务器') !== false) {
                $item = str_replace(' . ', '', $item);
                $index = strpos($item, '192');
                $ip = substr($item, $index);
            }
        }
        return $ip;
    }

注意事项:

1、禁用函数中,需要去除exec

2、linux系统可能是其他命令,需要将"ipconfig /all"替换成对应的命令行

3、返回结果,可能不带"DHCP 服务器"关键字,需要自行通过结果修改

4、正常命令行的结果不是utf8格式,需要自行转换格式

相关推荐
火柴就是我3 小时前
让我们实现一个更好看的内部阴影按钮
android·flutter
blasit6 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
砖厂小工9 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心10 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心10 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker12 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴13 小时前
Android17 为什么重写 MessageQueue
android
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android