Php get client mac address program code _ PHP Tutorial-php Tutorial

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

The code is as follows:

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.