How to get mac address of client machine in PHP

posted 4 min read

In order to know the answer to this problem first, we need to know what is Mac and IP address.

What is a MAC address, and what does it mean? The term MAC stands for "Media Access Control," and it is a 48-bit physical address that every networking device has. The data-link layer uses the MAC address to transport a data packet from source to destination. 

In this article, we will discuss about how to get the IP address of the connected client in PHP. PHP has a superglobal variable ie $_SERVER, which contains information about the header, path, and script locations. We will see how  ‘REMOTE_ADDR’ returns the client’s IP address which is the key to $_SERVER variable.

z13

Photo by Rachel Vine
TABLE OF CONTENTS
  1. Solution 1: Using echo GetMAC() function
  2. Solution 2: Using GetClientMac() function
  3. Solution 3:
  4. Solution 4: Using the command
  5. Solution 5: Getting client device's ip and mac address
  6. Solution 6: Checking user agent OS Linux or windows . If OS windows then use this code
  7. If OS is Linux Ubuntu or Linux then use this code
  8. Solution 7: For OS X
  9. Conclusion

Solution 1: Using echo GetMAC() function


    echo GetMAC();

    function GetMAC(){
        ob_start();
        system('getmac');
        $Content = ob_get_contents();
        ob_clean();
        return substr($Content, strpos($Content,'\')-20, 17);
    }

Solution 2: Using GetClientMac() function


    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;
    }

Solution 3:


    <!--?php
    $string=exec('getmac');
    $mac=substr($string, 0, 17); 
    echo $mac;
    ?-->

Slution 4: Using the command


    cmd ipconfig /all

    <!--?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;
    ?-->

Solution 5: Getting client device's 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];
    }

Solution 6: Checking user agent OS Linux or windows . If OS windows then use this code


    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;
    }

If OS is Linux Ubuntu or Linux then use this code


    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;
        }

Solution 7: For OS X


    //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;

Conclusion:

In this article, we discussed how to use PHP's built-in superglobal variable $_SERVER to get the user's IP address in the above code.

I believe you understand how we can obtain clients' MAC and IP addresses, as well as their machine IDs.

Hope it helped.

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

Just completed another large-scale WordPress migration — and the client left this

saqib_devmorph - Apr 7

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelskiverified - Apr 23

How to Remove Orphan Linux Boot Entries from Windows 11 UEFI

Igor Giamoniano - Coisa de Dev - Apr 14
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!