The mac address is the NIC address. Generally, the user's mac address location cannot be obtained, because the security limits the browser, so it is almost impossible to get it. Below we will take two pieces of code for you to play.
Example 1
PHP code:
The Code is as follows: |
Copy code |
<? Php @ Exec ("arp-a", $ array); // run the arp-a command and put the result in the array $ array. Echo "<pre> "; 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: |
Copy code |
<? Php 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 "<br/> ";
// Obtain the client // Linux $ Command = "arp-a {$ _ SERVER ['remote _ ADDR ']}"; Echo $ command; Echo "<br/> "; $ Result = '{$ command }';
// Windows $ Command = "nbtstat-a {$ _ SERVER ['remote _ ADDR ']}"; Echo $ command; Echo "<br/> "; $ 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.
The Code is as follows: |
Copy code |
<Script language = "JScript" event = "OnCompleted (hResult, pErrorObject, pAsyncContext)" for = "foo"> Document. forms [0]. lbMacAddr. value = unescape (MACAddr ); </Script> <Script language = "JScript" event = "OnObjectReady (objObject, objAsyncContext)" for = "foo"> If (objObject. IPEnabled! = Null & objObject. IPEnabled! = "Undefined" & objObject. IPEnabled = true & objObject. MACAddress! = Null & objObject. MACAddress! = "Undefined") MACAddr = objObject. MACAddress; </Script> <Object id = "locator" classid = "CLSID: 76A64158-CB41-11D1-8B02-00600806D9B6"> </object> <Object id = "foo" classid = "CLSID: 75718C9A-F029-11d1-A1AC-00C04FB6C223"> </object> <Script language = "JScript"> Var service = locator. ConnectServer (); Var MACAddr; Var IPAddr; Var DomainAddr; Var sDNSName; Service. Security _. ImpersonationLevel = 3; Service. InstancesOfAsync (foo, 'win32 _ networkadapterconfiguration '); </Script> <Form> <input type = "text" id = 'lbmacaddr 'name = 'lbmacaddr'/> </form> |
Only applicable to IE browser, and there will be an alarm