PHP获取MAC地址

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值