How to get MAC address of client using PHP?

117,883

Solution 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

Solution 2

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.

Solution 3

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

Solution 4

Here's a possible way to do it:

$string=exec('getmac');
$mac=substr($string, 0, 17); 
echo $mac;

Solution 5

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;
}
Share:
117,883

Related videos on Youtube

kim edgard
Author by

kim edgard

Updated on October 20, 2021

Comments

  • kim edgard
    kim edgard over 2 years

    How can I get MAC Address using PHP or javascript...

    • Pointy
      Pointy about 13 years
      You cannot do that.
    • Paul Schreiber
      Paul Schreiber about 13 years
      You should post that as an answer.
    • Steven
      Steven about 13 years
      Why is this getting down voted? I too was looking for this answer.
    • Buttle Butkus
      Buttle Butkus over 11 years
      Then 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.
    • Pointy
      Pointy over 11 years
      @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?
    • Buttle Butkus
      Buttle Butkus over 11 years
      @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.
    • Simply  Seth
      Simply Seth over 9 years
      $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
  • Ben O
    Ben O about 10 years
    PHP is serverside, this displays the servers details.
  • Makarand Mane
    Makarand Mane about 9 years
    for me it doesnot shown anything. I have linux hosting. no error, no warning, no output
  • Kristian Williams
    Kristian Williams over 8 years
    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
    Kristian Williams over 8 years
    Relying on client-side JavaScript and 3rd party Active X components is horribly insecure and outdated.
  • Blue
    Blue over 7 years
    While this code snippet may solve the question, including an explanation 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!
  • Patrick Fisher
    Patrick Fisher over 6 years
    Yes, it's a pretty horrible thing. It is also the only correct answer given so far.
  • Patrick Fisher
    Patrick Fisher over 6 years
    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
    Patrick Fisher over 6 years
    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.
  • Ankit Virani
    Ankit Virani over 6 years
    It's work on localhost server. Not working on live server
  • Pankaj Jha
    Pankaj Jha about 5 years
    This 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
  • catcon
    catcon over 3 years
    @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
  • phper
    phper about 2 years
    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.