这篇文章给大家介绍使用php怎么获取客户端的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("/[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>
关于使用php怎么获取客户端的mac地址就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。