How to detect the MAC address of a device in PHP

In this article, we will learn how to get the MAC address or physical address of a system using PHP.

First of all, let me clear the concept of the MAC address.

What is a MAC address?

It abbreviated as media access control. A MAC address is a unique id of a device in the network interface controller (NIC).

It is also known as a physical address, an Ethernet hardware address, and a hardware address.

Example:-




14-F6-D7-88-3E-29

How to get MAC address using system command in PHP

The system() command gathers the information from the configuration file (ipconfig file). From that information file, we have to access only the physical address. The script is as follows,

<?php  
   //Buffering the output
   ob_start();  
   
   //Getting configuration details 
   system('ipconfig /all');  
   
   //Storing output in a variable 
   $configdata=ob_get_contents();  
   
   // Clear the buffer  
   ob_clean();  
   
   //Extract only the physical address or Mac address from the output
   $mac = "Physical";  
   $pmac = strpos($configdata, $mac);
   
   // Get Physical Address  
   $macaddr=substr($configdata,($pmac+36),17);  
   
   //Display Mac Address  
   echo $macaddr;  
   
?>

Output:-

38-D6-E7-78-4E-29

In this way, we can extract the MAC address of a computer. If you have any doubts about this article, leave a comment below.

See also,


Leave a Reply

Your email address will not be published. Required fields are marked *