PHP擷取MAC地址

來源:互聯網
上載者:User

   擷取網卡的MAC地址原碼;目前支援WIN/LINUX系統 擷取機器網卡的物理(MAC)地址

 
  1. <?php  
  2. /**  
  3. 擷取網卡的MAC地址原碼;目前支援WIN/LINUX系統  
  4. 擷取機器網卡的物理(MAC)地址  
  5. **/  
  6.     
  7. class GetMacAddr{  
  8.     
  9.         var $return_array = array(); // 返回帶有MAC地址的字串數組  
  10.         var $mac_addr;  
  11.     
  12.         function GetMacAddr($os_type){  
  13.              switch ( strtolower($os_type) ){  
  14.                       case "linux":  
  15.                                 $this->forLinux();  
  16.                                 break;  
  17.                       case "solaris":  
  18.                                 break;  
  19.                       case "unix":  
  20.                                  break;  
  21.                        case "aix":  
  22.                                  break;  
  23.                        default:  
  24.                                  $this->forWindows();  
  25.                                  break;  
  26.      
  27.               }  
  28.      
  29.                 
  30.               $temp_array = array();  
  31.               foreach ( $this->return_array as $value ){  
  32.      
  33.                         if (  
  34. 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,  
  35. $temp_array ) ){  
  36.                                  $this->mac_addr = $temp_array[0];  
  37.                                  break;  
  38.                        }  
  39.      
  40.               }  
  41.               unset($temp_array);  
  42.               return $this->mac_addr;  
  43.          }  
  44.      
  45.      
  46.          function forWindows(){  
  47.               @exec("ipconfig /all", $this->return_array);  
  48.               if ( $this->return_array )  
  49.                        return $this->return_array;  
  50.               else{  
  51.                        $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";  
  52.                        if ( is_file($ipconfig) )  
  53.                           @exec($ipconfig." /all", $this->return_array);  
  54.                        else  
  55.                           @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);  
  56.                        return $this->return_array;  
  57.               }  
  58.          }  
  59.      
  60.      
  61.      
  62.          function forLinux(){  
  63.               @exec("ifconfig -a", $this->return_array);  
  64.               return $this->return_array;  
  65.          }  
  66.      
  67. }  
  68. //方法使用  
  69. //$mac = new GetMacAddr(PHP_OS);  
  70. //echo $mac->mac_addr;  
  71. ?> 




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.