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

时间:2023-01-05 20:24:23

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

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

12 个解决方案

#1


163  

Server IP

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

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

Server MAC address

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

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

Client IP address

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

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

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.

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

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).

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

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 addressforeach($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.

好吧,除非你能让客户自愿提供这些信息并通过其他方式传输,否则你运气不好。

#2


21  

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地址。

#3


8  

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。

#4


6  

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 -an command.

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

#5


6  

For windows server I think u can use this:

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

<?phpecho exec('getmac');?>

#6


1  

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.

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

#7


1  

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

#8


0  

You can get MAC Address or Physical Address using this code

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

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

我使用爆炸很多次因为shell_exec(“ipconfig / all”)返回所有网络的完整细节。所以你必须逐个拆分。当你运行这个代码然后你会得到你的MAC地址00 - ## - ## - CV-12 //这是假的地址仅供展示。

#9


0  

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;

#10


-2  

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.

在Linux下使用iptables你可以使用mac地址和ip.from php查找最后一项带有ip地址和获取mac地址的每个请求登录到文件。

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

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

#11


-2  

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.

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

You can write a simple PHP code using:

您可以使用以下命令编写简单的PHP代码:

 $localIP = getHostByName(getHostName()); 

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

之后,使用openWRT你可以去/tmp/dhcp.leases,你会得到一些表格:

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

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

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

#12


-3  

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

这适用于Windows,因为ipconfig / all是Windows系统命令。

#1


163  

Server IP

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

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

Server MAC address

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

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

Client IP address

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

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

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.

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

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).

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

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 addressforeach($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.

好吧,除非你能让客户自愿提供这些信息并通过其他方式传输,否则你运气不好。

#2


21  

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地址。

#3


8  

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。

#4


6  

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 -an command.

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

#5


6  

For windows server I think u can use this:

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

<?phpecho exec('getmac');?>

#6


1  

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.

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

#7


1  

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

#8


0  

You can get MAC Address or Physical Address using this code

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

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

我使用爆炸很多次因为shell_exec(“ipconfig / all”)返回所有网络的完整细节。所以你必须逐个拆分。当你运行这个代码然后你会得到你的MAC地址00 - ## - ## - CV-12 //这是假的地址仅供展示。

#9


0  

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;

#10


-2  

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.

在Linux下使用iptables你可以使用mac地址和ip.from php查找最后一项带有ip地址和获取mac地址的每个请求登录到文件。

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

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

#11


-2  

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.

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

You can write a simple PHP code using:

您可以使用以下命令编写简单的PHP代码:

 $localIP = getHostByName(getHostName()); 

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

之后,使用openWRT你可以去/tmp/dhcp.leases,你会得到一些表格:

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

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

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

#12


-3  

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

这适用于Windows,因为ipconfig / all是Windows系统命令。