欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-13 09:55:04
废话不多讲,直接上代码吧! 复制代码 代码如下:

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

复制代码 代码如下:

<?php  
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 "<br />";

// 获取客户端
// linux
$command = "arp -a {$_server['remote_addr']}";
echo $command;
echo "<br />";
$result=`{$command}`; 

// windows
$command = "nbtstat -a {$_server['remote_addr']}";
echo $command;
echo "<br />";
$result=`{$command}`; 
print_r($result);  
?>

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

复制代码 代码如下:

<script language="jscript" event="oncompleted(hresult,perrorobject, pasynccontext)" for="foo">   
document.forms[0].lbmacaddr.value=unescape(macaddr);   
</script>  
<script language="jscript" event="onobjectready(objobject,objasynccontext)" for="foo">   
if(objobject.ipenabled != null && objobject.ipenabled != "undefined" && objobject.ipenabled == true && objobject.macaddress != null && objobject.macaddress != "undefined") macaddr = objobject.macaddress;   
</script>  
<object id="locator" classid="clsid:76a64158-cb41-11d1-8b02-00600806d9b6"></object>  
<object id="foo" classid="clsid:75718c9a-f029-11d1-a1ac-00c04fb6c223"></object>  
<script language="jscript">   
var service = locator.connectserver();   
var macaddr ;   
var ipaddr ;   
var domainaddr;   
var sdnsname;   
service.security_.impersonationlevel=3;   
service.instancesofasync(foo, 'win32_networkadapterconfiguration');   
</script>  
<form><input type="text" id='lbmacaddr' name='lbmacaddr' /></form>

只适用于ie浏览器,而且会有告警提示,挺遗憾的。