php如何擷取網卡MAC地址(支援WIN與LINUX系統)

來源:互聯網
上載者:User

例子,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. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.