当前位置:首页 > PHP教程 > php应用 > 列表

php获取服务器端mac和客户端mac的地址支持WIN/LINUX

发布:smiling 来源: PHP粉丝网  添加日期:2020-12-16 15:25:59 浏览: 评论:0 

这篇文章主要介绍了php获取服务器端mac和客户端mac地址的方法,需要的朋友可以参考下。

获取服务器mac,代码如下:

  1. <?php 
  2. /** 
  3. 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 
  4. 获取机器网卡的物理(MAC)地址 
  5. **/ 
  6. class GetmacAddr{ 
  7. var $result = array(); // 返回带有MAC地址的字串数组 
  8. var $macAddr; 
  9. /*构造*/ 
  10. function __construct($osType){ 
  11. switch ( strtolower($osType) ){ 
  12. case "unix": break; 
  13. case "solaris": break; 
  14. case "aix": break; 
  15. case "linux": { 
  16. $this->for_linux_os(); 
  17. }break; 
  18. default: { 
  19. $this->for_windows_os(); 
  20. }break; 
  21. } 
  22. $temp_array = array(); 
  23. foreach($this->result as $value){ 
  24. if(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, 
  25. $temp_array ) ){ 
  26. $this->macAddr = $temp_array[0]; 
  27. break; 
  28. } 
  29. } 
  30. unset($temp_array); 
  31. return $this->macAddr; 
  32. } 
  33. /*linux系统中获取方法*/ 
  34. function for_linux_os(){ 
  35. @exec("ifconfig -a", $this->result); 
  36. return $this->result; 
  37. } 
  38. /*win系统中的获取方法*/ 
  39. function for_windows_os(){ 
  40. @exec("ipconfig /all", $this->result); 
  41. if ( $this->result ) { 
  42. return $this->result; 
  43. } else { 
  44. $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe"; 
  45. if(is_file($ipconfig)) { 
  46. @exec($ipconfig." /all", $this->result); 
  47. } else { 
  48. @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->result); 
  49. return $this->result; 
  50. } 
  51. } 
  52. } 
  53. } 
  54. ?> 

获取客户端mac地址:

  1. @exec("arp -a",$array); //执行arp -a命令,结果放到数组$array中 
  2. foreach($array as $value){ 
  3. //匹配结果放到数组$mac_array 
  4. if(strpos($value,$_SERVER["REMOTE_ADDR"]) && preg_match("/(:?[0-9A-F]{2}[:-]){5}[0-9A-F]{2}/i",$value,$mac_array)){ 
  5. $mac = $mac_array[0]; 
  6. break; 
  7. } 
  8. } 
  9. echo $mac; 

注:客户端获取的mac不能在本机测试,只能用别的电脑访问才能输出

Tags: php获取服务器端地址

分享到: