Home  >  Article  >  php教程  >  php/js获取客户端mac地址的实现代码

php/js获取客户端mac地址的实现代码

WBOY
WBOYOriginal
2016-06-06 20:28:281321browse

这篇文章主要介绍了如何在php与js中分别获取客户度mac地址的方法,需要的朋友可以参考下

废话不多讲,直接上代码吧!

复制代码 代码如下:


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 "
";

// 获取客户端
// 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);  
?>

获取服务端的逻辑没什么大问题,虚拟主机,可能会存在权限问题。
获取客户端的时候,可能会比较慢,arp/nbstat命令执行会比较慢。

复制代码 代码如下:


  
  
  
  
  


只适用于IE浏览器,而且会有告警提示,香港服务器,挺遗憾的。

,免备案空间
Statement:
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