Home  >  Article  >  Backend Development  >  PHP获取电脑网卡MAC地址

PHP获取电脑网卡MAC地址

WBOY
WBOYOriginal
2016-06-20 13:04:151447browse

获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址

<?php  <br />    /**  <br />    获取网卡的MAC地址原码;目前支持WIN/LINUX系统  <br />    获取机器网卡的物理(MAC)地址  <br />    **/  <br />        <br />    class GetMacAddr{  <br />        <br />            var $return_array = array(); // 返回带有MAC地址的字串数组  <br />            var $mac_addr;  <br />        <br />            function GetMacAddr($os_type){  <br />                 switch ( strtolower($os_type) ){  <br />                          case "linux":  <br />                                    $this->forLinux();  <br />                                    break;  <br />                          case "solaris":  <br />                                    break;  <br />                          case "unix":  <br />                                     break;  <br />                           case "aix":  <br />                                     break;  <br />                           default:  <br />                                     $this->forWindows();  <br />                                     break;  <br />         <br />                  }  <br />         <br />                    <br />                  $temp_array = array();  <br />                  foreach ( $this->return_array as $value ){  <br />         <br />                            if (  <br />    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,  <br />    $temp_array ) ){  <br />                                     $this->mac_addr = $temp_array[0];  <br />                                     break;  <br />                           }  <br />         <br />                  }  <br />                  unset($temp_array);  <br />                  return $this->mac_addr;  <br />             }  <br />         <br />         <br />             function forWindows(){  <br />                  @exec("ipconfig /all", $this->return_array);  <br />                  if ( $this->return_array )  <br />                           return $this->return_array;  <br />                  else{  <br />                           $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";  <br />                           if ( is_file($ipconfig) )  <br />                              @exec($ipconfig." /all", $this->return_array);  <br />                           else  <br />                              @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);  <br />                           return $this->return_array;  <br />                  }  <br />             }  <br />         <br />         <br />         <br />             function forLinux(){  <br />                  @exec("ifconfig -a", $this->return_array);  <br />                  return $this->return_array;  <br />             }  <br />         <br />    }  <br />//方法使用<br />$mac = new GetMacAddr(PHP_OS);  <br />echo $mac->mac_addr;  <br />?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn