How can I get MAC Address using PHP or javascript...
-
19You cannot do that. – Pointy Feb 22 '11 at 04:53
-
3You should post that as an answer. – Paul Schreiber Feb 22 '11 at 04:55
-
5Why is this getting down voted? I too was looking for this answer. – Steven Mar 25 '11 at 07:48
-
5Then vote it up. Some people have an irresistible urge to downvote questions when a.) they think the answer is obvious or b.) they think the answer to the question is "it's impossible" or c.) they can't think of a good reason to do what the asker is trying to do. – Buttle Butkus Jan 08 '13 at 21:45
-
@ButtleButkus I agree that there's a lot of inappropriate downvoting around here, but this question is hardly stellar. *What* MAC address is the OP interested in, for example? Why? What's the overall goal? – Pointy Jan 08 '13 at 22:29
-
6@Pointy the asker may not know the difference between a MAC address and an IP address. But I think people should worry less about the asker as a person and more about what the question asks. People searching the internet and finding this question may have all kinds of reasons for asking this exact question. Even if the asker's reason isn't good, theirs might be. Suggestions/admonitions can go in comments. Asker might realize he doesn't want to do this after all. But other people will find the question and answers useful, as I did. – Buttle Butkus Jan 09 '13 at 01:38
-
1$ip=$_SERVER['REMOTE_ADDR'];$arp=`arp $ip`; $lines=explode(" ",$arp);$macaddr=$lines[3]; this works on FreeBSD you might have to use arping on Linux ... this runs the "arp" command with the backticks – Simply Seth Oct 11 '14 at 06:48
11 Answers
The MAC address (the low-level local network interface address) does not survive hops through IP routers. You can't find the client MAC address from a remote server.
In a local subnet, the MAC addresses are mapped to IP addresses through the ARP system. Interfaces on the local net know how to map IP addresses to MAC addresses. However, when your packets have been routed on the local subnet to (and through) the gateway out to the "real" Internet, the originating MAC address is lost. Simplistically, each subnet-to-subnet hop of your packets involve the same sort of IP-to-MAC mapping for local routing in each subnet.

- 405,095
- 59
- 585
- 614
echo GetMAC();
function GetMAC(){
ob_start();
system('getmac');
$Content = ob_get_contents();
ob_clean();
return substr($Content, strpos($Content,'\\')-20, 17);
}
Above will basically execute the getmac
program and parse its console-output, resulting to MAC-address of the server (and/or where ever PHP
is installed and running on).

- 7,611
- 5
- 39
- 71

- 193
- 1
- 4
-
4While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Sep 30 '16 at 19:38
-
2
-
@Top-Master: I removed your edit since it's misleading. PHP is server side language and does not run on client side. Please have a look at this [Client side vs Server side](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – catcon Nov 03 '20 at 23:36
Here's a possible way to do it:
$string=exec('getmac');
$mac=substr($string, 0, 17);
echo $mac;

- 6,961
- 8
- 45
- 71

- 99
- 1
- 1
-
5This doesn't work in Linux System, and even if it work it gets you Mac address of the server which is executing the script, so No Client Mac Address – Pankaj Jha Mar 30 '19 at 11:53
Use this function to get the client MAC address:
function GetClientMac(){
$macAddr=false;
$arp=`arp -n`;
$lines=explode("\n", $arp);
foreach($lines as $line){
$cols=preg_split('/\s+/', trim($line));
if ($cols[0]==$_SERVER['REMOTE_ADDR']){
$macAddr=$cols[2];
}
}
return $macAddr;
}

- 57
- 1
- 4
To get client's device ip and mac address
{
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
$macCommandString = "arp " . $ipaddress . " | awk 'BEGIN{ i=1; } { i++; if(i==3) print $3 }'";
$mac = exec($macCommandString);
return ['ip' => $ipaddress, 'mac' => $mac];
}

- 614
- 1
- 7
- 20

- 31
- 1
You can get the client's MAC address in javascript, if they are running Windows and allow you to install an ActiveX control.
http://www.eggheadcafe.com/community/aspnet/3/10054371/how-to-get-client-mac-address.aspx
http://codingresource.blogspot.com/2010/02/get-client-mac-address-ip-address-using.html

- 7,926
- 5
- 35
- 28
-
3Relying on client-side JavaScript and 3rd party Active X components is horribly insecure and outdated. – Kristian Williams Jul 28 '15 at 11:37
-
4Yes, it's a pretty horrible thing. It is also the only correct answer given so far. – Patrick Fisher Aug 11 '17 at 20:07
-
I do suspect that the questioner does not know the difference between a MAC address and an IP address, but the fact remains that it is possible to get the MAC address of a remote client via the browser. It is a horribly insecure and useless thing to do, but it is possible. – Patrick Fisher Aug 11 '17 at 20:11
-
More likely the questioner meant to get the local MAC address, not the client. That is a very different question and the other answers here give solutions in PHP. – Patrick Fisher Aug 11 '17 at 20:14
...
function get_remote_macaddr( $ip ) {
return( strtoupper( exec( "arp -a " . $ip . " | awk '{print $4 }'") )
}
// test
$ipaddress = '192.168.20.252';
echo 'ip: '.$ipaddress.", ".'mac_addr: '.get_remote_macaddr( $ipaddress );

- 11
- 1
The idea is, using the command cmd ipconfig /all
and extract only the address mac.
Which his index $pmac+33.
And the size of mac is 17.
<?php
ob_start();
system('ipconfig /all');
$mycom=ob_get_contents();
ob_clean();
$findme = 'physique';
$pmac = strpos($mycom, $findme);
$mac=substr($mycom,($pmac+33),17);
echo $mac;
?>
<?php
ob_start();
system('ipconfig/all');
$mycom=ob_get_contents();
ob_clean();
$findme = "Physical";
$pmac = strpos($mycom, $findme);
$mac=substr($mycom,($pmac+36),17);
echo $mac;
?>
This prints the mac address of client machine

- 2,851
- 1
- 13
- 27

- 1
- 1
First you check your user agent OS Linux or windows or another. Then Your OS Windows Then this code use:
public function win_os(){
ob_start();
system('ipconfig-a');
$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
return $mac;
}
And your OS Linux Ubuntu or Linux then this code use:
public function unix_os(){
ob_start();
system('ifconfig -a');
$mycom = ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean (erase) the output buffer
$findme = "Physical";
//Find the position of Physical text
$pmac = strpos($mycom, $findme);
$mac = substr($mycom, ($pmac + 37), 18);
return $mac;
}
This code may be work OS X.

- 4,376
- 11
- 45
- 60

- 91
- 1
- 3
//Simple & effective way to get client mac address
// Turn on output buffering
ob_start();
//Get the ipconfig details using system commond
system('ipconfig /all');
// Capture the output into a variable
$mycom=ob_get_contents();
// Clean (erase) the output buffer
ob_clean();
$findme = "Physical";
//Search the "Physical" | Find the position of Physical text
$pmac = strpos($mycom, $findme);
// Get Physical Address
$mac=substr($mycom,($pmac+36),17);
//Display Mac Address
echo $mac;

- 33
- 1
-
4
-
for me it doesnot shown anything. I have linux hosting. no error, no warning, no output – Makarand Mane Jan 27 '15 at 11:07
-
ipconfig is Windows only, and this is server side. In the all real-world scenarios, it's impossible to get a client MAC address in PHP and there shouldn't be any case where you'd want to. – Kristian Williams Jul 28 '15 at 11:36
-
Of course there are use cases. I have a static IP address from my ISP and I use that to execute special (typically debugging) code on the server only when I am the user connected, based on my IP address. No other user will see that output. However when my ISP switches me to fiber they won't offer fixed or static IP addresses and I will need a way to do the same thing even though my IP address won't be the same every visit. My MAC address won't change so if my code can determine the user's MAC address and it is mine then it can safely execute that special code. – phper Jan 28 '22 at 19:42