150

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

4

16 回答 16

202

服务器 IP

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

服务器 MAC 地址

对于 MAC 地址,您可以netstat -ie在 Linux 或ipconfig /allWindows 中解析输出。

客户端 IP 地址

您可以从中获取客户端IP$_SERVER['REMOTE_ADDR']

客户端 MAC 地址

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

因此,如果您正在构建某种基于 LAN 的系统并且您的客户端位于同一以太网段上,那么您可以通过解析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];
   }
}

但是,如果客户端不在 LAN 上怎么办?

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

于 2009-09-14T08:48:33.303 回答
24

客户端的 MAC 地址(在发出 HTTP 请求的计算机的意义上)被客户端和服务器之间的每个路由器覆盖。

客户端 IP 方便地提供给$_SERVER['REMOTE_ADDR']. 在某些情况下,特别是如果您的 Web 服务器位于代理(即缓存代理)$_SERVER['REMOTE ADDR']之后,将返回代理的 IP 并且通常会有一个额外的值,$_SERVER['HTTP_X_FORWARDED_FOR']其中包含原始请求客户端的 IP。

有时,特别是当您处理不受您控制的匿名代理时,代理不会返回真实的 IP 地址,而您所希望的只是代理的 IP 地址。

于 2010-08-18T10:43:15.057 回答
9

我不认为你可以在 PHP 中获取 MAC 地址,但你可以从$_SERVER['REMOTE_ADDR']变量中获取 IP。

于 2009-09-14T08:49:06.283 回答
9

对于 Windows 服务器,我想你可以使用这个:

<?php
echo exec('getmac');
?>
于 2016-08-19T22:12:35.777 回答
7

您需要做的就是将 arp 放入不同的组中。

默认:

-rwxr-xr-x 1 root root 48K 2008-11-11 18:11 /usr/sbin/arp*

使用命令:

sudo chown root:www-data /usr/sbin/arp

你会得到:

-rwxr-xr-x 1 root www-data 48K 2008-11-11 18:11 /usr/sbin/arp*

并且因为 apache 是在用户 www-data 下运行的守护进程,所以它现在能够执行此命令。

因此,如果您现在使用 PHP 脚本,例如:

<?php
$mac = system('arp -an');
echo $mac;
?>

你会得到linuxarp -an命令的输出。

于 2010-07-16T12:02:30.353 回答
3

您可以 使用此代码获取MAC地址或 物理地址

$d = explode('Physical Address. . . . . . . . .',shell_exec ("ipconfig/all"));  
$d1 = explode(':',$d[1]);  
$d2 = explode(' ',$d1[1]);  
return $d2[1];

我多次使用爆炸,因为shell_exec ("ipconfig/all")返回所有网络的完整详细信息。所以你必须一一拆分。当您运行此代码时,您将获得
您的MAC 地址 00-##-##-CV-12 //这是仅用于显示的假地址。

于 2018-05-25T21:49:15.603 回答
2

使用这个类(https://github.com/BlakeGardner/php-mac-address)

这是一个 PHP 类,用于在 Unix、Linux 和 Mac OS X 操作系统之上进行 MAC 地址操作。它主要是为了帮助欺骗无线安全审计而编写的。

于 2013-11-05T11:17:41.377 回答
1

您可以使用以下解决方案来解决您的问题:

$mac='UNKNOWN';
foreach(explode("\n",str_replace(' ','',trim(`getmac`,"\n"))) as $i)
if(strpos($i,'Tcpip')>-1){$mac=substr($i,0,17);break;}
echo $mac;
于 2018-08-18T21:00:47.517 回答
1

在 Windows 中,如果用户在本地使用您的脚本,这将非常简单:

<?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); 
?>
于 2017-08-29T18:58:09.220 回答
0

使用 PHP 获取 MAC 地址:(在 Ubuntu 18.04 中测试)- 2020 更新

这是代码:

<?php
    $mac = shell_exec("ip link | awk '{print $2}'");
    preg_match_all('/([a-z0-9]+):\s+((?:[0-9a-f]{2}:){5}[0-9a-f]{2})/i', $mac, $matches);
    $output = array_combine($matches[1], $matches[2]);
    $mac_address_values =  json_encode($output, JSON_PRETTY_PRINT);   
    echo $mac_address_values
?>

输出:

{
    "lo": "00:00:00:00:00:00",
    "enp0s25": "00:21:cc:d4:2a:23",
    "wlp3s0": "84:3a:4b:03:3c:3a",
    "wwp0s20u4": "7a:e3:2a:de:66:09"
}
于 2020-01-27T11:17:30.800 回答
0

回答为时已晚,但这是我的方法,因为这里没有人提到这一点:
为什么不是客户端解决方案?
将mac存储在cookie中的javascript实现(您可以在此之前对其进行加密)
然后每个请求都必须包含该cookie名称,否则它将被拒绝。
为了使这更有趣,您可以
从您获得制造商的 mac 地址进行服务器端验证(有很多免费的 API 用于此),
然后将其与 user_agent 值进行比较以查看是否存在某种操作:
mac HP 的地址 + Safari 的用户代理 = 拒绝请求。

于 2020-08-12T18:54:40.537 回答
0

我们可以通过这种方式在 php 中获取 Ubuntu 中的 MAC 地址

$ipconfig =   shell_exec ("ifconfig -a | grep -Po 'HWaddr \K.*$'");  
    // display mac address   
 echo $ipconfig;
于 2019-10-14T08:00:17.150 回答
0
// 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;  

这在 Windows 上适用于我,ipconfig /allWindows 系统命令也是如此。

于 2015-11-05T06:04:28.357 回答
0

也许获取 Mac 地址并不是通过 Internet 验证客户端计算机的最佳方法。考虑改用令牌,该令牌由管理员登录存储在客户端的浏览器中。

因此,如果管理员通过浏览器将其授予客户端,则客户端只能拥有此令牌。如果令牌不存在或无效,则客户端的机器无效。

于 2020-02-21T01:20:01.513 回答
-1

在使用 iptables 的 linux 下,您可以使用 mac 地址和 ip 将每个对 Web 服务器的请求记录到一个文件中。从 php 查找带有 ip 地址的最后一项并获取 mac 地址。

如前所述,请记住 MAC 地址来自跟踪上的最后一个路由器。

于 2016-05-04T05:51:16.797 回答
-4

您可以使用 openWRT 轻松完成此操作。如果您使用强制门户,您可以混合使用 php 和 openWRT 并在 IP 和 mac 之间建立关系。

您可以使用以下方法编写简单的 PHP 代码:

 $localIP = getHostByName(getHostName()); 

稍后,您可以使用 openWRT 转到/tmp/dhcp.leases,您将获得以下表格:

 e4:a7:a0:29:xx:xx 10.239.3.XXX DESKTOP-XXX 

在那里,你有 mac、IP 地址和主机名。

于 2017-05-16T10:31:42.263 回答