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.
Image description
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);
    }
Enter fullscreen mode Exit fullscreen mode

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;
    }
Enter fullscreen mode Exit fullscreen mode

Solution 3:

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

Solution 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;
    ?-->
Enter fullscreen mode Exit fullscreen mode

If these solutions are not working for you, then try these solutions.

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.

Logo

更多推荐