php 获取服务器端mac地址,客户端mac地址

来源:互联网 发布:sitv网络电视 编辑:程序博客网 时间:2024/05/20 09:47
  1. //服务器端mac获取
  2. <?php     
  3. /**   
  4. 获取网卡的MAC地址原码;目前支持WIN/LINUX系统   
  5. 获取机器网卡的物理(MAC)地址 
  6. **/     
  7. class GetmacAddr{   
  8.     var $result = array(); // 返回带有MAC地址的字串数组   
  9.     var $macAddr;  
  10.     /*构造*/  
  11.     function __construct($osType){   
  12.         switch ( strtolower($osType) ){   
  13.             case "unix"break;  
  14.             case "solaris"break;  
  15.             case "aix"break;  
  16.             case "linux": {  
  17.                 $this->for_linux_os();  
  18.             }break;   
  19.             default: {   
  20.                 $this->for_windows_os();   
  21.             }break;   
  22.         }   
  23.         $temp_array = array();   
  24.         foreach($this->result as $value){  
  25.             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,   
  26.                 $temp_array ) ){   
  27.                 $this->macAddr = $temp_array[0];   
  28.                 break;   
  29.             }   
  30.         }   
  31.         unset($temp_array);   
  32.         return $this->macAddr;   
  33.     }  
  34.     /*linux系统中获取方法*/  
  35.     function for_linux_os(){   
  36.         @exec("ifconfig -a"$this->result);   
  37.         return $this->result;   
  38.     }  
  39.     /*win系统中的获取方法*/  
  40.     function for_windows_os(){   
  41.         @exec("ipconfig /all"$this->result);   
  42.         if ( $this->result ) {  
  43.             return $this->result;  
  44.         } else {   
  45.             $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";  
  46.             if(is_file($ipconfig)) {  
  47.                 @exec($ipconfig." /all"$this->result);  
  48.             } else {  
  49.                 @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all"$this->result);  
  50.                 return $this->result;   
  51.             }  
  52.         }   
  53.     }   
  54. }   
  55. ?>    
  56. //客户端mac获取
  57. @exec("arp -a",$array); //执行arp -a命令,结果放到数组$array中  
  58.         foreach($array as $value){  
  59.             //匹配结果放到数组$mac_array  
  60.             if(strpos($value,$_SERVER["REMOTE_ADDR"]) && preg_match("/(:?[0-9A-F]{2}[:-]){5}[0-9A-F]{2}/i",$value,$mac_array)){  
  61.                 $mac = $mac_array[0];  
  62.                 break;  
  63.             }  
  64.         }  
  65.         echo $mac

0 0