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

WBOY
發布: 2016-07-25 09:12:54
原創
1001 人瀏覽過

例子,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. ?>
复制代码


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!