This article describes how to obtain the mac address of the server and the mac address of the client in php. For more information, see obtain the mac address of the server.
The code is as follows:
/**
Obtain the MAC address source code of the NIC. Currently, Windows/LINUX systems are supported.
Obtain the physical (MAC) address of the machine Nic
**/
Class GetmacAddr {
Var $ result = array (); // returns a string array with a MAC address
Var $ macAddr;
/* Construct */
Function _ construct ($ osType ){
Switch (strtolower ($ osType )){
Case "unix": break;
Case "solaris": break;
Case "aix": break;
Case "linux ":{
$ This-> for_linux_ OS ();
} Break;
Default :{
$ This-> for_windows_ OS ();
} Break;
}
$ Temp_array = array ();
Foreach ($ this-> result 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-> macAddr = $ temp_array [0];
Break;
}
}
Unset ($ temp_array );
Return $ this-> macAddr;
}
/* Obtain methods in linux */
Function for_linux_ OS (){
@ Exec ("ifconfig-a", $ this-> result );
Return $ this-> result;
}
/* Obtain methods in win system */
Function for_windows_ OS (){
@ Exec ("ipconfig/all", $ this-> result );
If ($ this-> result ){
Return $ this-> result;
} Else {
$ Ipconfig = $ _ SERVER ["WINDIR"]. "\ system32 \ ipconfig.exe ";
If (is_file ($ ipconfig )){
@ Exec ($ ipconfig. "/all", $ this-> result );
} Else {
@ Exec ($ _ SERVER ["WINDIR"]. "\ system \ ipconfig.exe/all", $ this-> result );
Return $ this-> result;
}
}
}
}
?>
Obtain the client mac address:
The code is as follows:
@ Exec ("arp-a", $ array); // run the arp-a command and put the result in the array $ array.
Foreach ($ array as $ value ){
// Put the matching result in the array $ mac_array
If (strpos ($ value, $ _ SERVER ["REMOTE_ADDR"]) & preg_match ("/(:? [0-9A-F] {2} [:-]) {5} [0-9A-F] {2}/I ", $ value, $ mac_array )){
$ Mac = $ mac_array [0];
Break;
}
}
Echo $ mac;
Note: The mac obtained by the client cannot be tested on the local machine. only access from other computers can be used for output.