PHP retrieves an instance of a MAC address
Share a php code for obtaining the mac address of a machine and learn how to read hardware information from php. This Code is applicable to windows and linux systems. For more information, see.
The php code shared in this section has the following functions:
Obtain the physical (MAC) Address of the machine Nic.
Code:
The Code is as follows:
<? Php
/**
* Obtain the physical (MAC) Address of the machine Nic
* Currently, Windows/LINUX systems are supported.
**/
Class MacAddInfo {
Var $ return_array = array (); // returns a string array with a MAC address.
Var $ mac_addr;
Function MacAddInfo ($ OS _type ){
Switch (strtolower ($ OS _type )){
Case "linux ":
$ This-> forLinux ();
Break;
Case "solaris ":
Break;
Case "unix ":
Break;
Case "aix ":
Break;
Default:
$ This-> forWindows ();
Break;
}
$ Temp_array = array ();
Foreach ($ this-> return_array as $ value ){
If (preg_match ("/[0-9a-f] [0-9a-f] [:-]". "[0-9a-f] [0-9a-f] [:-]". "[0-9a-f] [0-9a-f] [:-]". "[0-9a-f] [0-9a-f] [:-]". "[0-9a-f] [0-9a-f] [:-]". "[0-9a-f] [0-9a-f]/I", $ value, $ temp_array )){
$ This-> mac_addr = $ temp_array [0];
Break;
}
}
Unset ($ temp_array );
Return $ this-> mac_addr;
}
Function forWindows (){
@ Exec ("ipconfig/all", $ this-> return_array );
If ($ this-> return_array)
Return $ this-> return_array;
Else {
$ Ipconfig = $ _ SERVER ["WINDIR"]. "/system32/ipconfig.exe ";
If (is_file ($ ipconfig ))
@ Exec ($ ipconfig. "/all", $ this-> return_array );
Else
@ Exec ($ _ SERVER ["WINDIR"]. "/system/ipconfig.exe/all", $ this-> return_array );
Return $ this-> return_array;
}
}
Function forLinux (){
@ Exec ("ifconfig-a", $ this-> return_array );
Return $ this-> return_array;
}
}
// Call example
// $ Mac = new MacAddInfo (PHP_ OS );
// Echo $ mac-> mac_addr;
?>