views:

2918

answers:

6

I want to get the MAC and IP address of the connected client using php.

+8  A: 

IP

echo $_SERVER['REMOTE_ADDR'];

MAC

You can't get the remote MAC address because it's not transmitted to your server.

Georg
+7  A: 

You can get the server IP address from $_SERVER['SERVER_ADDR']. For the MAC address, you could parse the output of netstat -ie in Linux, or ipconfig /all in Windows.

You can get the client IP from $_SERVER['REMOTE_ADDR'], but the client MAC address will not be available (unless the client is on the same ethernet segment as the server!)

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 ($col[0]==$ipAddress)
   {
       $macAddr=$col[1];
   }
}
Paul Dixon
How to get output of arp -a by using php??
Neveen
+1  A: 

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

RaYell
A: 

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

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 its now able to execute this command.

so if u now use php script e.g.

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

you will get the output of linux arp -an command

McJ
A: 

The MAC address of a client (in the sense of the computer that issued a HTTP request) is overwritten by every router between the client and the server.

Pies
A: 

You hit the PHP limitation.

You cannot get MAC address using netstat OR ARP.

ARP cannot return MACs of IPs beyond the router you are connected to.

NETSTAT is usually blocked at the target.

Best of luck!

eeswar
Not exactly a "PHP limitation", more of a "how networks work" limitation.
deceze