对于 MAC 地址,您可以在 Linux 中解析 netstat -ie的输出,或在 Windows 中解析 ipconfig /all的输出。
客户端 IP 地址
您可以从 $_SERVER['REMOTE_ADDR']获得客户端 IP
客户端 MAC 地址
客户端 MAC 地址将不可用于您,除非在一个特殊的情况: 如果客户端与服务器在同一个以太网段上。
因此,如果您正在构建某种基于局域网的系统,并且您的客户机 是位于同一个以太网段上,那么您可以通过解析 arp -n(linux)或 arp -a(windows)的输出来获得 MAC 地址。
编辑 : 您在注释中询问如何获取外部命令的输出-一种方法是使用反勾,例如。
$ipAddress=$_SERVER['REMOTE_ADDR'];
$macAddr=false;
#run the external command, break output into lines
$arp=`arp -a $ipAddress`;
$lines=explode("\n", $arp);
#look for the output line describing our IP address
foreach($lines as $line)
{
$cols=preg_split('/\s+/', trim($line));
if ($cols[0]==$ipAddress)
{
$macAddr=$cols[1];
}
}
// Turn on output buffering
ob_start();
//Get the ipconfig details using system commond
system('ipconfig /all');
// Capture the output into a variable
$mycomsys=ob_get_contents();
// Clean (erase) the output buffer
ob_clean();
$find_mac = "Physical";
//find the "Physical" & Find the position of Physical text
$pmac = strpos($mycomsys, $find_mac);
// Get Physical Address
$macaddress=substr($mycomsys,($pmac+36),17);
//Display Mac Address
echo $macaddress;
<?php
// get all the informations about the client's network
$ipconfig = shell_exec ("ipconfig/all"));
// display those informations
echo $ipconfig;
/*
look for the value of "physical adress" and use substr() function to
retrieve the adress from this long string.
here in my case i'm using a french cmd.
you can change the numbers according adress mac position in the string.
*/
echo substr(shell_exec ("ipconfig/all"),1821,18);
?>
<?php
// PHP code to get the MAC address of Server
$MAC = exec('getmac');
// Storing 'getmac' value in $MAC
$MAC = strtok($MAC, ' ');
// Updating $MAC value using strtok function,
// strtok is used to split the string into tokens
// split character of strtok is defined as a space
// because getmac returns transport name after
// MAC address
echo "MAC address of Server is: $MAC";
?>