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

Neveen picture Neveen · Sep 14, 2009 · Viewed 421.6k times · Source

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

Answer

Paul Dixon picture Paul Dixon · Sep 14, 2009

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.