This question already has an answer here:
这个问题在这里已有答案:
- How can I get the MAC and the IP address of a connected client in PHP? 12 answers
- How to get the client IP address in PHP? 23 answers
如何在PHP中获取已连接客户端的MAC和IP地址? 12个答案
如何在PHP中获取客户端IP地址? 23个答案
I was trying to get IP and MAC address of client using PHP but I am unable to get that. I tried below code but it gives wrong MAC address -
我试图使用PHP获取客户端的IP和MAC地址,但我无法得到它。我尝试下面的代码,但它提供错误的MAC地址 -
ob_start(); // Turn on output buffering
system(‘ipconfig /all’);
$mycom=ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean (erase) the output buffer
$findme = "Physical";
$pmac = strpos($mycom, $findme);
$mac=substr($mycom,($pmac+36),17); // Get Physical Address
2 个解决方案
#1
0
Like the commenters say on the question, you will not be able to get the client MAC since the PHP runs on the server, but you can get the client IP by checking $_SERVER['REMOTE_ADDR']
or if the user is behind a proxy, you can use $_SERVER['HTTP_X_FORWARDED_FOR']
就像评论者在问题上所说的那样,你将无法获得客户端MAC,因为PHP在服务器上运行,但你可以通过检查$ _SERVER ['REMOTE_ADDR']或用户是否在代理后面来获取客户端IP ,你可以使用$ _SERVER ['HTTP_X_FORWARDED_FOR']
#2
0
As mentioned by many users its not possible to get user MAC_ADDRESS
. To get user ip try this
正如许多用户所提到的,它不可能获得用户MAC_ADDRESS。要获得用户ip,请尝试这样做
function getUserIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
#1
0
Like the commenters say on the question, you will not be able to get the client MAC since the PHP runs on the server, but you can get the client IP by checking $_SERVER['REMOTE_ADDR']
or if the user is behind a proxy, you can use $_SERVER['HTTP_X_FORWARDED_FOR']
就像评论者在问题上所说的那样,你将无法获得客户端MAC,因为PHP在服务器上运行,但你可以通过检查$ _SERVER ['REMOTE_ADDR']或用户是否在代理后面来获取客户端IP ,你可以使用$ _SERVER ['HTTP_X_FORWARDED_FOR']
#2
0
As mentioned by many users its not possible to get user MAC_ADDRESS
. To get user ip try this
正如许多用户所提到的,它不可能获得用户MAC_ADDRESS。要获得用户ip,请尝试这样做
function getUserIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}