PHP获取MAC地址

来源:互联网 发布:mysql emoji 编辑:程序博客网 时间:2024/05/22 03:51
参考文章:http://www.cnblogs.com/nackman/archive/2012/07/22/2603443.html说明:我在自己使用后,对源码进行了部分更改,原始代码请参考原文;我只在win7和Ubuntu上测试过源码如下:<?phpclass GetMacAddr{    var $return_array = array(); // 返回带有MAC地址的字串数组    var $mac_addr = array();    function GetMacAddr($os_type){        switch ( strtolower($os_type) ){            case "linux":                $this->forLinux();                break;            case "solaris":                break;            case "unix":                break;            case "aix":                break;            default:                $this->forWindows();                break;        }        $temp_array = array();//数组,用来保存匹配正则表达式的mac地址        //用来匹配mac地址的正则表达式        $preg_string = "/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]".            "[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]".            "[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i";        foreach ( $this->return_array as $value )        {            if (preg_match($preg_string, $value, $temp_array ) )            {                array_push( $this->mac_addr, $temp_array[0]);            }        }        unset($temp_array);        return $this->mac_addr;    }    //获取运行Windows操作系统的计算机mac地址    function forWindows(){        @exec("ipconfig /all", $this->return_array);        if ( $this->return_array )            return $this->return_array;        else{            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";            if ( is_file($ipconfig) )                @exec($ipconfig." /all", $this->return_array);            else                @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all",                    $this->return_array);        }    }    //获取运行Linux操作系统的计算机mac地址    function forLinux(){        @exec("ifconfig -a", $this->return_array);    }}?>GetMacAddr类的使用方法:include 'getMacAddr.php';$mac = new GetMacAddr('windows');echo $mac->mac_addr[0];//此处数组下标根据实际情况定。                        //在win7下存在不止一个本地网络连接的时候,就要自己来确定哪个是真正的MAC地址。我在Linux上部署遇到的问题:exec() has been disabled for security reasons in ....原因:出于安全考虑,php禁用了exec()函数。参考:http://blog.csdn.net/hnllc2012/article/details/48649837解决方法:修改php.ini,使exec()可用。参考:http://blog.csdn.net/hnllc2012/article/details/48649607
补充:windows系统下,获取MAC地址也可以使用getmac命令。有兴趣话的可以自己尝试下。
不同系统下获取MAC地址的方法:http://www.unixmantra.com/2013/04/how-to-finddisplay-your-mac-address.html
0 0
原创粉丝点击