当前位置:Gxlcms > PHP教程 > php如何获取网卡MAC地址(支持WIN与LINUX系统)

php如何获取网卡MAC地址(支持WIN与LINUX系统)

时间:2021-07-01 10:21:17 帮助过:15人阅读

例子,php获取网卡的物理地址,即mac地址。

  1. /**

  2. 获取网卡的MAC地址;目前支持WIN/LINUX系统
  3. 获取机器网卡的物理(MAC)地址
  4. **/

  5. class GetMacAddr{

  6. var $return_array = array(); // 返回带有MAC地址的字串数组
  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. //方法使用
  54. $mac = new GetMacAddr(PHP_OS);
  55. echo $mac->mac_addr; //机器的真实MAC地址,请注释掉
  56. ?>

人气教程排行