PHP获取物理网卡地址,获取网卡名称。

jackxiang 2009-6-29 17:06 | |
获取网卡名称:


代码:

<?php
/**
        获取机器网卡的物理(MAC)地址
        wwa  $Exp
**/
class GetMacAddr
{
        var $return_array = array(); // 返回带有MAC地址的字串数组
        var $mac_addr;
      
        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();
                foreach ( $this->return_array as $value )
                {
                        if ( preg_match( "/[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", $value, $temp_array ) )
                        {
                                $this->mac_addr = $temp_array[0];
                                break;
                        }
                }
                unset($temp_array);
                return $this->mac_addr;
        }

        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);
                        return $this->return_array;
                }
        }

        function forLinux()
        {
                @exec("ifconfig -a", $this->return_array);
                return $this->return_array;
        }
}
?>
<?php
$mac = new GetMacAddr(PHP_OS);
echo $mac->mac_addr;
?>


作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/1807/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2018-3-12 14:56
评论列表
2010-10-8 16:41 | wang Email Homepage
获取服务器的mac地址有何用呢?
分页: 1/1 第一页 1 最后页
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]