1. 程式人生 > >JS配合PHP獲取客戶端MAC地址

JS配合PHP獲取客戶端MAC地址

<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>
php:
<?php
class GetMacAddr
{
        var $return_array = array(); 
        var $mac_addr;
        
        function GetMacAddr($os_type)
        {
                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->return_array 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->mac_addr = $temp_array[0];
                                break;
                        }
                }
                unset($temp_array);
                return $this->mac_addr;
        }
        function forWindows()
        {
                @exec("ipconfig /all", $this->return_array);
                if ( $this->return_array )
                        return $this->return_array;
                else{
                        $ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";
                        if ( is_file($ipconfig) )
                                @exec($ipconfig." /all", $this->return_array);
                        else
                                @exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->return_array);
                        return $this->return_array;
                }
        }
        function forLinux()
        {
                @exec("ifconfig -a", $this->return_array);
                return $this->return_array;
        }
}
?>
<?
$mac = new GetMacAddr(PHP_OS);
echo $mac->mac_addr;
//獲取客戶端
/*$result=`arp -a $REMOTE_ADDR`;
//$result=`nbtstat -a $REMOTE_ADDR`;
print_r($result);*/
?>