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

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

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

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

11 个解决方案

#1


155  

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

你真倒霉,除非你能让客户自愿提供信息并通过其他方式传递。

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

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

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.

在$_SERVER['REMOTE_ADDR']中,可以方便地将客户端IP提供给脚本。在某些场景中,特别是如果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服务器,我认为你可以这样使用:

<?php
echo 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.

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

#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


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

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

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

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

#10


-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。租赁,你会得到一些表格:

 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地址和主机名。

#11


-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


155  

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

你真倒霉,除非你能让客户自愿提供信息并通过其他方式传递。

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

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

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.

在$_SERVER['REMOTE_ADDR']中,可以方便地将客户端IP提供给脚本。在某些场景中,特别是如果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服务器,我认为你可以这样使用:

<?php
echo 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.

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

#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


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

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

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

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

#10


-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。租赁,你会得到一些表格:

 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地址和主机名。

#11


-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系统命令。