如何在 PHP 中获取已连接客户端的 MAC 和 IP 地址?

发布于 2025-01-12 18:27:23 字数 46 浏览 3 评论 0原文

我需要知道连接客户端的 MAC 和 IP 地址,如何在 PHP 中执行此操作?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

提赋 2025-01-19 18:27:23

服务器IP

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

服务器 MAC 地址

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

客户端 IP 地址

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

客户端 MAC 地址

客户端 MAC 地址将不可用,除非在一种特殊情况下:如果客户端是与服务器在同一以太网段上。

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

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

$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 上怎么办?

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

Server IP

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

Server MAC address

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

Client IP address

You can get the client IP from $_SERVER['REMOTE_ADDR']

Client MAC address

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.

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("\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];
   }
}

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.

不疑不惑不回忆 2025-01-19 18:27:23

客户端的 MAC 地址(就发出 HTTP 请求的计算机而言)会被客户端和服务器之间的每个路由器覆盖。

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

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

The MAC address of a client (in the sense of the computer that issued the HTTP request) is overwritten by every router between the client and the server.

Client IP is conveniently provided to the script in $_SERVER['REMOTE_ADDR']. In some scenarios, particularly if your web server is behind a proxy (i.e. a caching proxy) $_SERVER['REMOTE ADDR'] will return the IP of the proxy, and there will be an extra value, often $_SERVER['HTTP_X_FORWARDED_FOR'], that contains the IP of the original request client.

Sometimes, particularly when you're dealing with an anonymizing proxy that you don't control, the proxy won't return the real IP address, and all you can hope for is the IP address of the proxy.

梦幻的心爱 2025-01-19 18:27:23

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

I don't think you can get MAC address in PHP, but you can get IP from $_SERVER['REMOTE_ADDR'] variable.

故事灯 2025-01-19 18:27:23

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

<?php
echo exec('getmac');
?>

For windows server I think u can use this:

<?php
echo exec('getmac');
?>
不弃不离 2025-01-19 18:27:23

您所需要做的就是将 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;
?>

您将获得 linux arp -an 命令的输出。

All you need to do is to put arp into diferrent group.

Default:

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

With command:

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

you will get:

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

And because apache is a daemon running under the user www-data, it's now able to execute this command.

So if you now use a PHP script, e.g.:

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

you will get the output of linux arp -an command.

む无字情书 2025-01-19 18:27:23

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

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

Use this class (https://github.com/BlakeGardner/php-mac-address)

This is a PHP class for MAC address manipulation on top of Unix, Linux and Mac OS X operating systems. it was primarily written to help with spoofing for wireless security audits.

梦里泪两行 2025-01-19 18:27:23

在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); 
?>

In windows, If the user is using your script locally, it will be very simple :

<?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); 
?>
风柔一江水 2025-01-19 18:27:23

代码获取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 //这是仅供展示的假地址。

You can get MAC Address or Physical Address using this code

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

I used explode many time because shell_exec ("ipconfig/all") return complete detail of all network. so you have to split one by one.
when you run this code then you will get
your MAC Address 00-##-##-CV-12 //this is fake address for show only.

走野 2025-01-19 18:27:23

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

too late to answer but here is my approach since no one mentioned this here:
why not a client side solution ?
a javascript implementation to store the mac in a cookie (you can encrypt it before that)
then each request must include that cookie name, else it will be rejected.
to make this even more fun you can make a server side verification
from the mac address you get the manifacturer (there are plenty of free APIs for this)
then compare it with the user_agent value to see if there was some sort of manipulation:
a mac address of HP + a user agent of Safari = reject request.

迷路的信 2025-01-19 18:27:23

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

$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;

You can use the following solution to solve your problem:

$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;
北方的韩爷 2025-01-19 18:27:23

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

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

Perhaps getting the Mac address is not the best approach for verifying a client's machine over the internet. Consider using a token instead which is stored in the client's browser by an administrator's login.

Therefore the client can only have this token if the administrator grants it to them through their browser. If the token is not present or valid then the client's machine is invalid.

前事休说 2025-01-19 18:27:23

这对我有用:

<?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";
?>

来源:https://www.geeksforgeeks.org/how-to-get-the-mac-and-ip-address-of-a-connected-client-in-php/

This one works for me:

<?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";
?>

Source: https://www.geeksforgeeks.org/how-to-get-the-mac-and-ip-address-of-a-connected-client-in-php/

乱世争霸 2025-01-19 18:27:23
// 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 /all 是 Windows 系统命令。

// 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;  

This works for me on Windows, as ipconfig /all is Windows system command.

鸩远一方 2025-01-19 18:27:23

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

$ipconfig =   shell_exec ("ifconfig -a | grep -Po 'HWaddr \K.*
");  
    // display mac address   
 echo $ipconfig;

We can get MAC address in Ubuntu by this ways in php

$ipconfig =   shell_exec ("ifconfig -a | grep -Po 'HWaddr \K.*
");  
    // display mac address   
 echo $ipconfig;
悟红尘 2025-01-19 18:27:23

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

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

under linux using iptables you can log to a file each request to web server with mac address and ip.
from php lookup last item with ip address and get mac address.

As stated remember that the mac address is from last router on the trace.

情深缘浅 2025-01-19 18:27:23

您可以使用 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 地址和主机名。

You can do this easily using openWRT. If yo use a captive portal you can mix php and openWRT and make a relation between the IP and the mac.

You can write a simple PHP code using:

 $localIP = getHostByName(getHostName()); 

Later, using openWRT you can go to /tmp/dhcp.leases, you will get something with the form:

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

There, you have the mac, the IP address and the hostname.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文