Home  >  Article  >  Backend Development  >  How to obtain the network card MAC address in php (supports WIN and LINUX systems)

How to obtain the network card MAC address in php (supports WIN and LINUX systems)

WBOY
WBOYOriginal
2016-07-25 09:12:541083browse

Example, php gets the physical address of the network card, which is the mac address.

  1. /**

  2. Get the MAC address of the network card; currently supports WIN/LINUX system
  3. Get the physical (MAC) address of the machine's network card
  4. **/

  5. class GetMacAddr{

  6. var $return_array = array(); // Return the band String array with MAC address
  7. var $mac_addr;

  8. function GetMacAddr($os_type){

  9. switch ( strtolower($os_type) ){
  10. case "linux":
  11. $this-> ;forLinux();
  12. break;
  13. case "solaris":
  14. break;
  15. case "unix":
  16. break;
  17. case "aix":
  18. break;
  19. default:
  20. $this->forWindows();
  21. break ;

  22. }

  23. $temp_array = array();

  24. foreach ( $this->return_array as $value ){

  25. < ;p>if (
  26. 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,
  27. $temp_array ) ){
  28. $this->mac_addr = $temp_array[0];
  29. break;
  30. } bbs.it-home.org

  31. }

  32. unset($temp_array);
  33. return $this->mac_addr;
  34. }
  35. function forWindows(){

  36. @exec("ipconfig /all", $this->return_array);
  37. if ( $this->return_array )
  38. return $this->return_array;
  39. else {
  40. $ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";
  41. if ( is_file($ipconfig) )
  42. @exec($ipconfig." /all", $this->return_array);
  43. else
  44. @exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->return_array);
  45. return $this->return_array;
  46. }
  47. }

  48. @exec("ifconfig -a", $this->return_array);
  49. return $this->return_array;
  50. }

  51. }

  52. //Method Use
  53. $mac = new GetMacAddr(PHP_OS);
  54. echo $mac->mac_addr; //The real MAC address of the machine, please comment out
  55. ?>
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