PHP - how to get mac address of device

Asked By Bhanuprakash T.R on 29-Aug-11 10:17 PM
Hello Sir,
  i am working android phonegap ..... i want get the mac id of android device using php or javascript... please send the sample code for that


Thanks
Bhanu prakash
Anoop S replied to Bhanuprakash T.R on 30-Aug-11 12:56 AM

We can find out the MAC Address of our Android phones or other devices like this.

    * click on the Home button at the bottom of the phone to go to the Home screen.
    * Click Menu
    * Click Settings
    * Click About Phone
    * Click Status
    * Scroll down to see your “WLAN MAC address”
Reena Jain replied to Bhanuprakash T.R on 30-Aug-11 06:06 AM
Hi,

I don't think you can find the MAC address directly from PHP - it's not something that that is included in the http headers that a browser sends, so will not be present in server variables.

There does seem to be software available to find a MAC address given a IP address (google for "find mac address ip") but it only seems to work over a local network.


if you really want his MAC address one solution would be to shell out some command line tools and parse their output, on *nix I think doing this would get the mac address:

$mac = 'ping $ip && arp -a | grep $ip'

The output may have to be parsed, and I'm unsure if you can include varibles inside backticks
Devil Scorpio replied to Bhanuprakash T.R on 30-Aug-11 02:40 PM
Hi,

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

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];
   }
}
Devil Scorpio replied to Bhanuprakash T.R on 31-Aug-11 05:44 AM
Hi,

Use this code


<?
php
/*
* Getting MAC Address using PHP
* Md. Nazmul Basher
*/
 
ob_start(); // Turn on output buffering
system(‘ipconfig /all’); //Execute external program to display output
$mycom=ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean (erase) the output buffer
 
$findme = “Physical”;
$pmac = strpos($mycom, $findme); // Find the position of Physical text
$mac=substr($mycom,($pmac+36),17); // Get Physical Address
 
echo $mac;
?>