如何在 PHP 中获取已连接客户端的 MAC 和 IP 地址? [英] How can I get the MAC and the IP address of a connected client in PHP?

查看:54
本文介绍了如何在 PHP 中获取已连接客户端的 MAC 和 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道连接客户端的 MAC 和 IP 地址,我如何在 PHP 中做到这一点?

I need to know the MAC and the IP address of the connect clients, how can I do this in PHP?

推荐答案

服务器IP

您可以从 $_SERVER['SERVER_ADDR'] 获取服务器 IP 地址.

Server IP

You can get the server IP address from $_SERVER['SERVER_ADDR'].

对于MAC地址,Linux下可以解析netstat -ie的输出,Windows下可以解析ipconfig/all的输出.

For the MAC address, you could parse the output of netstat -ie in Linux, or ipconfig /all in Windows.

你可以从$_SERVER['REMOTE_ADDR']

除非在一种特殊情况下,否则您将无法使用客户端 MAC 地址:如果客户端与服务器位于同一以太网段.

The client MAC address will not be available to you except in one special circumstance: if the client is on the same ethernet segment as the server.

因此,如果您正在构建某种基于 LAN 的系统并且您的客户端 位于同一以太网段上,那么您可以通过解析 arp -n 的输出来获取 MAC 地址 (linux) 或 arp -a (windows).

So, if you are building some kind of LAN based system and your clients are on the same ethernet segment, then you could get the MAC address by parsing the output of arp -n (linux) or arp -a (windows).

编辑:您在评论中询问如何获取外部命令的输出 - 一种方法是使用反引号,例如

Edit: you ask in comments how to get the output of an external command - one way is to use backticks, e.g.

$ipAddress=$_SERVER['REMOTE_ADDR'];
$macAddr=false;

#run the external command, break output into lines
$arp=`arp -a $ipAddress`;
$lines=explode("
", $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];
   }
}

但是如果客户端不在 LAN 上呢?

好吧,除非您可以让客户自愿提供该信息并通过其他方式传输,否则您就不走运了.

But what if the client isn't on a LAN?

Well, you're out of luck unless you can have the client volunteer that information and transmit via other means.

这篇关于如何在 PHP 中获取已连接客户端的 MAC 和 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆