Php obtains the client mac address program code. The mac address is the Nic address. Generally, the user's mac address cannot be obtained, because the security limits the browser, so it is almost impossible to obtain the mac address, the following two pieces of code are used to show you that the mac address is the Nic address. Generally, the user's mac address cannot be obtained because the security restricts the browser, so it is almost impossible to obtain the mac address, here are two pieces of code for you to play.
Example 1
PHP code:
The code is as follows: |
|
@ Exec ("arp-a", $ array); // run the arp-a command and put the result in the array $ array. Echo""; Print_r ($ array); // Print the obtained array Foreach ($ array as $ value) { If (// put the matching result in the array $ mac_array 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; // output client MAC ?> |
Example 2
The code is as follows: |
|
Class MacAddr { Public $ returnArray = array (); Public $ macAddr; Function _ contruct ($ OS _type = null ){ If (is_null ($ OS _type) $ OS _type = PHP_ OS; 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-> returnArray 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; } Function forWindows (){ @ Exec ("ipconfig/all", $ this-> returnArray ); If ($ this-> returnArray) Return $ this-> returnArray; Else { $ Ipconfig = $ _ SERVER ["WINDIR"]. "system32ipconfig.exe "; If (is_file ($ ipconfig )) @ Exec ($ ipconfig. "/all", $ this-> returnArray ); Else @ Exec ($ _ SERVER ["WINDIR"]. "systemipconfig.exe/all", $ this-> returnArray ); Return $ this-> returnArray; } } Function forLinux (){ @ Exec ("ifconfig-a", $ this-> returnArray ); Return $ this-> returnArray; } } $ Mac = new MacAddr (PHP_ OS ); Echo $ mac-> macAddr; Echo" "; // Obtain the client // Linux $ Command = "arp-a {$ _ SERVER ['remote _ ADDR ']}"; Echo $ command; Echo" "; $ Result = '{$ command }'; // Windows $ Command = "nbtstat-a {$ _ SERVER ['remote _ ADDR ']}"; Echo $ command; Echo" "; $ Result = '{$ command }'; Print_r ($ result ); ?> |
There is no major problem in obtaining the server logic, and there may be permission issues.
When obtaining the client, it may be slow, and arp/nbstat command execution will be slow.