php 如何使用PHP获取客户端的MAC地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5074139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get MAC address of client using PHP?
提问by kim edgard
How can I get MAC Address using PHP or javascript...
如何使用 PHP 或 javascript 获取 MAC 地址...
采纳答案by Patrick Fisher
You can get the client's MAC address in javascript, if they are running Windows and allow you to install an ActiveX control.
如果客户端运行的是 Windows 并允许您安装 ActiveX 控件,则您可以在 javascript 中获取客户端的 MAC 地址。
http://www.eggheadcafe.com/community/aspnet/3/10054371/how-to-get-client-mac-address.aspx
http://www.eggheadcafe.com/community/aspnet/3/10054371/how-to-get-client-mac-address.aspx
http://codingresource.blogspot.com/2010/02/get-client-mac-address-ip-address-using.html
http://codingresource.blogspot.com/2010/02/get-client-mac-address-ip-address-using.html
回答by Pointy
The MAC address (the low-level local network interface address) does not survive hops through IP routers. You can't find the client MAC address from a remote server.
MAC 地址(低级本地网络接口地址)不会在通过 IP 路由器的跃点中保留下来。您无法从远程服务器找到客户端 MAC 地址。
In a local subnet, the MAC addresses are mapped to IP addresses through the ARP system. Interfaces on the local net know how to map IP addresses to MAC addresses. However, when your packets have been routed on the local subnet to (and through) the gateway out to the "real" Internet, the originating MAC address is lost. Simplistically, each subnet-to-subnet hop of your packets involve the same sort of IP-to-MAC mapping for localrouting in each subnet.
在本地子网中,MAC 地址通过 ARP 系统映射到 IP 地址。本地网络上的接口知道如何将 IP 地址映射到 MAC 地址。但是,当您的数据包在本地子网上路由到(并通过)网关到“真实”互联网时,原始 MAC 地址就会丢失。简单地说,数据包的每个子网到子网跃点都涉及相同类型的 IP 到 MAC 映射,用于每个子网中的本地路由。
回答by AbdulRahman AlShamiri
echo GetMAC();
function GetMAC(){
ob_start();
system('getmac');
$Content = ob_get_contents();
ob_clean();
return substr($Content, strpos($Content,'\')-20, 17);
}
回答by Rigat Dan Perez Donayre
Here's a possible way to do it:
这是一种可能的方法:
$string=exec('getmac');
$mac=substr($string, 0, 17);
echo $mac;
回答by ram ganesh
Use this function to get the client MAC address:
使用此函数获取客户端 MAC 地址:
function GetClientMac(){
$macAddr=false;
$arp=`arp -n`;
$lines=explode("\n", $arp);
foreach($lines as $line){
$cols=preg_split('/\s+/', trim($line));
if ($cols[0]==$_SERVER['REMOTE_ADDR']){
$macAddr=$cols[2];
}
}
return $macAddr;
}
回答by mounir
The idea is, using the command cmd ipconfig /all
and extract only the address mac.
这个想法是,使用命令cmd ipconfig /all
并只提取地址mac。
Which his index $pmac+33.
其中他的索引为$pmac+33。
And the size of mac is 17.
而mac的大小是17。
<?php
ob_start();
system('ipconfig /all');
$mycom=ob_get_contents();
ob_clean();
$findme = 'physique';
$pmac = strpos($mycom, $findme);
$mac=substr($mycom,($pmac+33),17);
echo $mac;
?>
回答by Rajarathinam
To get client's device ip and mac address
获取客户端的设备ip和mac地址
{
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
$macCommandString = "arp " . $ipaddress . " | awk 'BEGIN{ i=1; } { i++; if(i==3) print }'";
$mac = exec($macCommandString);
return ['ip' => $ipaddress, 'mac' => $mac];
}
回答by krishna prasanth
<?php
ob_start();
system('ipconfig/all');
$mycom=ob_get_contents();
ob_clean();
$findme = "Physical";
$pmac = strpos($mycom, $findme);
$mac=substr($mycom,($pmac+36),17);
echo $mac;
?>
This prints the mac address of client machine
这将打印客户端机器的 mac 地址
回答by Karim Khan
First you check your user agent OS Linux or windows or another. Then Your OS Windows Then this code use:
首先,您检查您的用户代理操作系统 Linux 或 Windows 或其他。然后你的操作系统 Windows 然后这段代码使用:
public function win_os(){
ob_start();
system('ipconfig-a');
$mycom=ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean (erase) the output buffer
$findme = "Physical";
$pmac = strpos($mycom, $findme); // Find the position of Physical text
$mac=substr($mycom,($pmac+36),17); // Get Physical Address
return $mac;
}
And your OS Linux Ubuntu or Linux then this code use:
而您的操作系统 Linux Ubuntu 或 Linux 那么此代码使用:
public function unix_os(){
ob_start();
system('ifconfig -a');
$mycom = ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean (erase) the output buffer
$findme = "Physical";
//Find the position of Physical text
$pmac = strpos($mycom, $findme);
$mac = substr($mycom, ($pmac + 37), 18);
return $mac;
}
This code may be work OS X.
此代码可能适用于 OS X。
回答by user3376118
//Simple & effective way to get client mac address
// Turn on output buffering
ob_start();
//Get the ipconfig details using system commond
system('ipconfig /all');
// Capture the output into a variable
$mycom=ob_get_contents();
// Clean (erase) the output buffer
ob_clean();
$findme = "Physical";
//Search the "Physical" | Find the position of Physical text
$pmac = strpos($mycom, $findme);
// Get Physical Address
$mac=substr($mycom,($pmac+36),17);
//Display Mac Address
echo $mac;