熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php/js獲取客戶端mac地址的實現代碼

2013-11-15 12:26:17  來源: PHP編程 
這篇文章主要介紹了如何在php與js中分別獲取客戶度mac地址的方法需要的朋友可以參考下  

  廢話不多講直接上代碼吧!

復制代碼 代碼如下:
<?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("/[af][af][:]""[af][af][:]""[af][af][:]""[af][af][:]""[af][af][:]""[af][af]/i" $value $temp_array)){  
                $this>macAddr = $temp_array[];  
                break;  
            }  
        }  
        unset($temp_array);  
        return $this>macAddr;  
    }

    function forWindows(){  
        @exec("ipconfig /all" $this>returnArray);  
        if($this>returnArray)  
            return $this>returnArray;  
        else{  
            $ipconfig = $_SERVER["WINDIR"]"systemipconfigexe";  
            if (is_file($ipconfig))  
                @exec($ipconfig" /all" $this>returnArray);  
            else 
                @exec($_SERVER["WINDIR"]"systemipconfigexe /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(hResultpErrorObject pAsyncContext)" for="foo">   
documentforms[]lbMacAddrvalue=unescape(MACAddr);   
</script>  
<script language="JScript" event="OnObjectReady(objObjectobjAsyncContext)" for="foo">   
if(objObjectIPEnabled != null && objObjectIPEnabled != "undefined" && objObjectIPEnabled == true && objObjectMACAddress != null && objObjectMACAddress != "undefined") MACAddr = objObjectMACAddress;   
</script>  
<object id="locator" classid="CLSID:ACBDBDB"></object>  
<object id="foo" classid="CLSID:CAFdAACCFBC"></object>  
<script language="JScript">   
var service = locatorConnectServer();   
var MACAddr ;   
var IPAddr ;   
var DomainAddr;   
var sDNSName;   
serviceSecurity_ImpersonationLevel=;   
serviceInstancesOfAsync(foo Win_NetworkAdapterConfiguration);   
</script>  
<form><input type="text" id=lbMacAddr name=lbMacAddr /></form>

  只適用於IE浏覽器而且會有告警提示挺遺憾的


From:http://tw.wingwit.com/Article/program/PHP/201311/20952.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.