PHP获取MAC地址的实现代码

原创
2016-07-25 08:56:08 1022浏览
  1. /**

  2. * 获取机器网卡的物理(MAC)地址
  3. * 目前支持WIN/LINUX系统
  4. * 编辑: bbs.it-home.org
  5. **/
  6. class MacAddInfo {
  7. var $return_array = array (); // 返回带有MAC地址的字串数组
  8. var $mac_addr;
  9. function MacAddInfo($os_type) {
  10. switch (strtolower ( $os_type )) {
  11. case "linux" :
  12. $this->forLinux ();
  13. break;
  14. case "solaris" :
  15. break;
  16. case "unix" :
  17. break;
  18. case "aix" :
  19. break;
  20. default :
  21. $this->forWindows ();
  22. break;
  23. }
  24. $temp_array = array ();

  25. foreach ( $this->return_array as $value ) {
  26. 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 )) {
  27. $this->mac_addr = $temp_array [0];
  28. break;
  29. }
  30. }
  31. unset ( $temp_array );
  32. return $this->mac_addr;
  33. }
  34. function forWindows() {
  35. @exec ( "ipconfig /all", $this->return_array );
  36. if ($this->return_array)
  37. return $this->return_array;
  38. else {
  39. $ipconfig = $_SERVER ["WINDIR"] . "\system32\ipconfig.exe";
  40. if (is_file ( $ipconfig ))
  41. @exec ( $ipconfig . " /all", $this->return_array );
  42. else
  43. @exec ( $_SERVER ["WINDIR"] . "\system\ipconfig.exe /all", $this->return_array );
  44. return $this->return_array;
  45. }
  46. }
  47. function forLinux() {
  48. @exec ( "ifconfig -a", $this->return_array );
  49. return $this->return_array;
  50. }
  51. }
  52. //调用示例
  53. //$mac = new MacAddInfo(PHP_OS);
  54. //echo $mac->mac_addr;
  55. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。