php获取客户端mac地址程序代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-27 14:39:15 浏览: 评论:0
mac地址是网卡地址一般情况下是无法获取到用户的mac地址地的,因为安全限制了浏览器所以几乎不太可能获取到了,下面拿两段代码给大家玩玩.
例1,PHP代码如下:
- <?php
- @exec("arp -a",$array); //执行arp -a命令,结果放到数组$array中
- echo "<pre>";
- print_r($array); //打印获取的数组
- foreach($array as $value)
- {
- if( //匹配结果放到数组$mac_array
- strpos($value,$_SERVER["REMOTE_ADDR"]) &&
- preg_match("/(:?[0-9a-f]{2}[:-]){5}[0-9a-f]{2}/i",$value,$mac_array)
- )//开源代码phpfensi.com
- {
- $mac = $mac_array[0];
- break;
- }
- }
- echo $mac; //输出客户端MAC
- ?>
例2,代码如下:
- <?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浏览器,而且会有告警提示.
Tags: php客户端 代码 mac地址程序
相关文章
- ·PHP实现检测客户端是否使用代理服务器及其匿名级别(2021-05-06)
- ·详解php与ethereum客户端交互(2021-09-15)
- ·自动发布新闻的php代码(2013-12-08)
- ·php 分析rss代码一段(2013-12-08)
- ·php的把ubb代码转换成html代码的程序(2013-12-09)
- ·php测试代码执行消耗的内存和时间(2014-01-06)
- ·phpmailer发送邮件代码(2014-01-21)
- ·php 发送邮件与pop3邮件登录代码(2014-01-21)
- ·php fsockopen邮箱发送实例代码(2014-01-22)
- ·301重定向代码(php apache) (2014-05-13)
- ·php 打印乘法口绝表代码(2014-05-19)
- ·PHP连接Memcache程序代码(2014-06-10)
- ·php读取本地php文件源代码输出显示(2014-06-14)
- ·PHP编程之代码命名规范(2014-06-14)
- ·php版淘宝网查询商品接口代码(2014-06-17)
- ·PHP把16进制的编码转为中文程序代码(2014-06-21)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)