How does php obtain the MAC address of the NIC (Windows and LINUX are supported)

Source: Internet
Author: User
How does php obtain the MAC address of the NIC (Windows and LINUX are supported)

For example, php obtains the physical address of the NIC, that is, the mac address.

  1. /**

  2. Obtain the MAC address of the NIC. Currently, Windows/LINUX are supported.
  3. Obtain the physical (MAC) address of the machine Nic
  4. **/

  5. Class GetMacAddr {

  6. Var $ return_array = array (); // returns a string array with a 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. 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"]. "\ system32 \ ipconfig.exe ";
  41. If (is_file ($ ipconfig ))
  42. @ Exec ($ ipconfig. "/all", $ this-> return_array );
  43. Else
  44. @ Exec ($ _ SERVER ["WINDIR"]. "\ system \ ipconfig.exe/all", $ this-> return_array );
  45. Return $ this-> return_array;
  46. }
  47. }

  48. Function forLinux (){

  49. @ Exec ("ifconfig-a", $ this-> return_array );
  50. Return $ this-> return_array;
  51. }

  52. }

  53. // Method usage
  54. $ Mac = new GetMacAddr (PHP_ OS );
  55. Echo $ mac-> mac_addr; // The actual MAC address of the machine. comment out the address.
  56. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.