Php obtains the mac address of the client Nic

Source: Internet
Author: User
Php obtains the mac address of the client Nic

In php programming, the user's mac address can be obtained to bind to the customer's computer and prevent junk registration.
A very simple class. you only need to print its macAddr attribute After instantiation.

Code:

  1. Class Getmac {
  2. Var $ result = array (); // returns a string array with a MAC address
  3. Var $ macAddr;
  4. /* Construct */
  5. Function _ construct ($ osType ){
  6. Switch (strtolower ($ osType )){
  7. Case "unix": break;
  8. Case "solaris": break;
  9. Case "aix": break;
  10. Case "linux ":{
  11. $ This-> for_linux_ OS ();
  12. } Break;
  13. Default :{
  14. $ This-> for_windows_ OS ();
  15. } Break;
  16. }
  17. $ Temp_array = array ();
  18. Foreach ($ this-> result as $ value ){
  19. 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,
  20. $ Temp_array )){
  21. $ This-> macAddr = $ temp_array [0];
  22. Break;
  23. }
  24. }
  25. Unset ($ temp_array );
  26. Return $ this-> macAddr;
  27. }
  28. /* Obtain methods in linux */
  29. Function for_linux_ OS (){
  30. @ Exec ("ifconfig-a", $ this-> result );
  31. Return $ this-> result;
  32. }
  33. /* Obtain methods in win system */
  34. Function for_windows_ OS (){
  35. @ Exec ("ipconfig/all", $ this-> result );
  36. If ($ this-> result ){
  37. Return $ this-> result;
  38. } Else {
  39. $ Ipconfig = $ _ SERVER ["WINDIR"]. "\ system32 \ ipconfig.exe ";
  40. If (is_file ($ ipconfig )){
  41. @ Exec ($ ipconfig. "/all", $ this-> result );
  42. } Else {
  43. @ Exec ($ _ SERVER ["WINDIR"]. "\ system \ ipconfig.exe/all", $ this-> result );
  44. Return $ this-> result;
  45. }
  46. }
  47. }
  48. }
  49. /* 1. implementation class 2. access its macAddr attribute directly */
  50. $ GetMac = new Getmac (PHP_ OS );
  51. Echo $ getMac-> macAddr;
  52. ?>



Client, Nic

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.