PHP中如何得到网卡MAC地址

此文章资料完全从由互联网英文站点得到,当初为了回答网友问题,写得和引用的英文。都很简单,希望大家有耐心看下去。
<!--StartFragment --> first i will share some basic thing of MAC address on internet.what happens?
look :

Because of the way that the internet was designed and the way that the IP protocol works, you wouldn't be able to find a mac address of someone unless you were local to them. The post earlier about using ping and arp is correct. The mac address is put into the IP packet at the 2nd level of the OSI model, its a physical address... once you go out through a router the mac address of the reported packet is changed, and is changed each and every time.. think of the mac as a "Return" address. You send out a packet and it goes across the country, and goes through 5 routers, the mac address on the packet will change each and every time it goes through a router, where the IP address won't. It's a type of routing code that the routers use to identify where the packet came from and where to send a response to.

example

Machine A sends a packet to Machine B, and it passes through Firewall C and D. The packet does this:

Machine A sends the packet to firewall C, firwall C repackages the packet with its mac address and looks for the quickest route to machine B, it determines that firewall D is the closest to it, so it repackages the packet with its own mac address (since that is where D will send a response to if it cant get to machine<!--emo&B)--> smile_blackeye.gif<!--endemo--> and sends it on to D.. D does the exact same thing and sends the packet to B. Once Machine B receives the packet the entire process starts over again, the last mac address that is reported is for firewall D, but the IP points to Machine A, thats how it knows where to go.


but there is still a function!!
function returnMacAddress() {
// This code is under the GNU Public Licence
// Written by michael_stankiewicz {don't spam} at yahoo {no spam} dot com
// Tested only on linux, please report bugs

// WARNING: the commands 'which' and 'arp' should be executable
// by the apache user; on most linux boxes the default configuration
// should work fine

// Get the arp executable path
$location = `which arp`;
// Execute the arp command and store the output in $arpTable
$arpTable = `$location`;
// Split the output so every line is an entry of the $arpSplitted array
$arpSplitted = split("\n",$arpTable);
// Get the remote ip address (the ip address of the client, the browser)
$remoteIp = $GLOBALS['REMOTE_ADDR'];
// Cicle the array to find the match with the remote ip address
foreach ($arpSplitted as $value) {
// Split every arp line, this is done in case the format of the arp
// command output is a bit different than expected
$valueSplitted = split(" ",$value);
foreach ($valueSplitted as $spLine) {
if (preg_match("/$remoteIp/",$spLine)) {
$ipFound = true;
}
// The ip address has been found, now rescan all the string
// to get the mac address
if ($ipFound) {
// Rescan all the string, in case the mac address, in the string
// returned by arp, comes before the ip address
// (you know, Murphy's laws)
reset($valueSplitted);
foreach ($valueSplitted as $spLine) {
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",$spLine)) {
return $spLine;
}
}
}
$ipFound = false;
}
}
return false;
}
<!--Signature-->
<!--StartFragment --> as mentioned before, be sure to understand the basics of the arp table since you can be sure that the obtained mac address is the right one only if the client is connected DIRECTLY to the php server,
if there are routers or gateways that NAT the traffic, the returned mac address will be that of the gateway, not the client beside it
<!--Signature-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值