找回密码
 立即注册
搜索
热搜: 生活 中国

mac地址

[复制链接]
admin 发表于 2017-3-15 15:57:58 | 显示全部楼层 |阅读模式
  1. <?php
  2. class GetMac{
  3. var $result = array();
  4. var $macAddrs = array(); //所有mac地址
  5. var $macAddr; //第一个mac地址
  6. function __construct($OS){
  7. $this->GetMac($OS);
  8. }
  9. function GetMac($OS){
  10. switch ( strtolower($OS) ){
  11. case "unix": break;
  12. case "solaris": break;
  13. case "aix": break;
  14. case "linux":
  15. $this->getLinux();
  16. break;
  17. default:
  18. $this->getWindows();
  19. break;
  20. }
  21. $tem = array();
  22. foreach($this->result as $val){
  23. 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",$val,$tem) ){
  24. $this->macAddr = $tem[0];//多个网卡时,会返回第一个网卡的mac地址,一般够用。
  25. break;
  26. //$this->macAddrs[] = $temp_array[0];//返回所有的mac地址
  27. }
  28. }
  29. unset($temp_array);
  30. return $this->macAddr;
  31. }
  32. //Linux系统
  33. function getLinux(){
  34. @exec("ifconfig -a", $this->result);
  35. return $this->result;
  36. }
  37. //Windows系统
  38. function getWindows(){
  39. @exec("ipconfig /all", $this->result);
  40. if ( $this->result ) {
  41. return $this->result;
  42. } else {
  43. $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
  44. if(is_file($ipconfig)) {
  45. @exec($ipconfig." /all", $this->result);
  46. } else {
  47. @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->result);
  48. return $this->result;
  49. }
  50. }
  51. }
  52. }
  53. $obj = new GetMac(PHP_OS);
  54. print_r($obj->result);
  55. echo $obj->macAddr;
  56. //获取客户端
  57. //$result=`arp -a $REMOTE_ADDR`;
  58. //$result=`nbtstat -a $REMOTE_ADDR`;
  59. //print_r($result);
  60. exit;
  61. ?>
复制代码
  1. <?php
  2. class GetMacAddr{
  3. var $return_array = array(); // 返回带有MAC地址的字串数组
  4. var $mac_addr;
  5. function GetMacAddr($os_type){
  6. switch(strtolower($os_type)){
  7. case "linux":$this->forLinux();break;
  8. default:$this->forWindows();break;
  9. }
  10. $temp_array = array();
  11. foreach ( $this->return_array as $value ){
  12. 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, $temp_array ) ) {
  13. $this->mac_addr = $temp_array[0];
  14. break;
  15. }
  16. }
  17. unset($temp_array);
  18. return $this->mac_addr;
  19. }
  20. function forWindows() {
  21. @exec("ipconfig /all", $this->return_array);
  22. if ( $this->return_array )
  23. return $this->return_array;
  24. else{
  25. $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
  26. if ( is_file($ipconfig) )
  27. @exec($ipconfig." /all", $this->return_array);
  28. else
  29. @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
  30. return $this->return_array;
  31. }
  32. }
  33. }
  34. //---实例化类
  35. $mac = new GetMacAddr(PHP_OS);
  36. echo $mac->mac_addr;
  37. echo "<br />";
  38. echo md5($mac->mac_addr);
  39. ?>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生活导航|生活导航 ( 新ICP备12003026-1号 )

GMT+8, 2024-4-27 00:55 , Processed in 0.081704 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表