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.

TABLE OF CONTENTS
- Solution 1: Using echo GetMAC() function
- Solution 2: Using GetClientMac() function
- Solution 3:
- Solution 4: Using the command
- Solution 5: Getting client device's ip and mac address
- Solution 6: Checking user agent OS Linux or windows . If OS windows then use this code
- If OS is Linux Ubuntu or Linux then use this code
- Solution 7: For OS X
- 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.