Home > Backend Development > PHP Tutorial > PHP gets the client mac address program code_PHP tutorial

PHP gets the client mac address program code_PHP tutorial

WBOY
Release: 2016-07-13 10:46:50
Original
1368 people have browsed it

The mac address is the network card address. Generally, the user's mac address cannot be obtained. Because of security restrictions on the browser, it is almost impossible to obtain it. Here are two pieces of code for everyone to play with.

Example 1

PHP code:

 代码如下 复制代码
@exec("arp -a",$array); //执行arp -a命令,结果放到数组$array中
echo "
";
  print_r($array); //打印获取的数组
 
  foreach($array as $value)
  {
      if( //匹配结果放到数组$mac_array
      strpos($value,$_SERVER["REMOTE_ADDR"]) &&
      preg_match("/(:?[0-9a-f]{2}[:-]){5}[0-9a-f]{2}/i",$value,$mac_array)
      )
      {
          $mac = $mac_array[0];
          break;
      }
  }
  echo $mac; //输出客户端MAC
?>


Example 2

 代码如下 复制代码

class MacAddr
{  
    public $returnArray = array();   
    public $macAddr;  
 
    function __contruct($os_type=null){
        if(is_null($os_type)) $os_type = PHP_OS;  
        switch (strtolower($os_type)){  
        case "linux":  
            $this->forLinux();  
            break;  
        case "solaris":  
            break;  
        case "unix":  
            break;  
        case "aix":  
            break;  
        default:  
            $this->forWindows();  
            break;  
        }  
        $temp_array = array();  
        foreach($this->returnArray as $value ){  
            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)){  
                $this->macAddr = $temp_array[0];  
                break;  
            }  
        }  
        unset($temp_array);  
        return $this->macAddr;  
    }
 
    function forWindows(){  
        @exec("ipconfig /all", $this->returnArray);  
        if($this->returnArray)  
            return $this->returnArray;  
        else{  
            $ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";  
            if (is_file($ipconfig))  
                @exec($ipconfig." /all", $this->returnArray);  
            else 
                @exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->returnArray);  
            return $this->returnArray;  
        }  
    }
 
    function forLinux(){  
        @exec("ifconfig -a", $this->returnArray);  
        return $this->returnArray;  
    }  
}

$mac = new MacAddr(PHP_OS);
echo $mac->macAddr;
echo "
";

// Get client
// linux
$command = "arp -a {$_SERVER['REMOTE_ADDR']}";
echo $command;
echo "
";
$result=`{$command}`;

// windows
$command = "nbtstat -a {$_SERVER['REMOTE_ADDR']}";
echo $command;
echo "
";
$result=`{$command}`;
print_r($result);
?>

There is no big problem in getting the server-side logic, but there may be permission issues.
When obtaining the client, it may be slow, and the execution of the arp/nbstat command will be slow.

Only applicable to IE browser, and there will be a warning prompt http://www.bkjia.com/PHPjc/632908.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632908.htmlTechArticleThe mac address is the network card address. Generally, the user's mac address cannot be obtained because security restricts browsing. It is almost impossible to obtain the device, so I will give you two pieces of code below...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
The code is as follows
 代码如下 复制代码

  
  
  
  
  

Copy code