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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1420381/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:30:33  来源:igfitidea点击:

How can I get the MAC and the IP address of a connected client in PHP?

phpmac-address

提问by Neveen

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

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

回答by Paul Dixon

Server IP

服务器IP

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

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

Server MAC address

服务器 MAC 地址

For the MAC address, you could parse the output of netstat -iein Linux, or ipconfig /allin Windows.

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

Client IP address

客户端 IP 地址

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

您可以从 $_SERVER['REMOTE_ADDR']

Client MAC address

客户端 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.

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

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

所以,如果你正在构建某种基于局域网的系统和你的客户在同一个以太网段,那么你可以通过解析的输出中获得的MAC地址arp -n(Linux)或arp -a(窗口)。

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?

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

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

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

回答by Micha? Tatarynowicz

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.

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

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.

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

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.

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

回答by RaYell

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

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

回答by Simos

For windows server I think u can use this:

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

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

回答by McJ

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

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

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.

因为 apache 是一个在用户 www-data 下运行的守护进程,它现在可以执行这个命令。

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

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

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

you will get the output of linux arp -ancommand.

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

回答by Ali

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

使用这个类(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.

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

回答by Mirtov

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

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

回答by Ajay

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

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

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

回答by Smack Alpha

Getting MAC Address Using PHP: (Tested in Ubuntu 18.04) - 2020 Update

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

Here's the Code:

这是代码:

<?php
    $mac = shell_exec("ip link | awk '{print }'");
    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
?>

Output:

输出:

{
    "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"
}

回答by Rosue Walford

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.

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

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.

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