Home >Backend Development >PHP Tutorial >Get the physical (MAC) address of the machine's network card

Get the physical (MAC) address of the machine's network card

WBOY
WBOYOriginal
2016-07-25 09:09:52984browse
  1. /**
  2. Get the physical (MAC) address of the machine’s network card
  3. **/
  4. class GetMacAddr
  5. {
  6. var $return_array = array(); // Return a string array with MAC address
  7. var $mac_addr;
  8. function GetMacAddr($os_type)
  9. {
  10. switch ( strtolower($os_type) )
  11. {
  12. case "linux ":
  13. $this-> forLinux();
  14. break;
  15. case "solaris ":
  16. break;
  17. case "unix ":
  18. break;
  19. case "aix ":
  20. break;
  21. default:
  22. $this-> forWindows();
  23. break;
  24. }
  25. $temp_array = array();
  26. foreach ( $this-> return_array as $value )
  27. {
  28. 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 ) )
  29. {
  30. $this-> ; mac_addr = $temp_array[0];
  31. break;
  32. }
  33. }
  34. unset($temp_array);
  35. return $this-> mac_addr;
  36. }
  37. function forWindows()
  38. {
  39. @exec( "ipconfig /all ", $this-> return_array);
  40. if ( $this-> return_array )
  41. return $this-> return_array;
  42. else{
  43. $ipconfig = $_SERVER[ "WINDIR "]. "system32ipconfig.exe ";
  44. if ( is_file($ipconfig) )
  45. @exec($ipconfig. " /all ", $this-> return_array);
  46. else
  47. @exec($_SERVER[ "WINDIR "]. "systemipconfig.exe /all " , $this-> return_array);
  48. return $this-> return_array;
  49. }
  50. }
  51. function forLinux()
  52. {
  53. @exec( "ifconfig -a ", $this-> return_array);
  54. return $this-> return_array;
  55. }
  56. }
  57. ?>
  58. $mac = new GetMacAddr(PHP_OS);
  59. echo $mac-> mac_addr;
  60. ?>
Copy code


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
Previous article:form validationNext article:form validation