Home  >  Article  >  Backend Development  >  PHP implementation code to obtain MAC address

PHP implementation code to obtain MAC address

WBOY
WBOYOriginal
2016-07-25 08:56:081291browse
  1. /**

  2. * Obtain the physical (MAC) address of the machine's network card
  3. * Currently supports WIN/LINUX system
  4. * Editor: 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"] . "system32ipconfig.exe";
  40. if (is_file ( $ipconfig ))
  41. @exec ( $ipconfig . " /all", $this->return_array );
  42. else
  43. @exec ( $_SERVER ["WINDIR"] . "systemipconfig.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. ?>

复制代码


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