Php obtains the mac address of the server and the mac address of the client. Windows/LINUX is supported.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.