PHP get client Nic Mac Physical Address

Source: Internet
Author: User

In the PHP programming, obtains the user MAC address, can realize with the client computer's binding, prevents the garbage registration and so on.
A very simple class that, when used, simply prints its Macaddr property when it is instantiated.

Code:

  1. Class getmac{
  2. var $result = array (); Returns an array of strings with MAC addresses
  3. var $macAddr;
  4. /* Construction */
  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. How to obtain the/*linux system */
  29. function For_linux_os () {
  30. @exec ("Ifconfig-a", $this->result);
  31. return $this->result;
  32. }
  33. Acquisition method 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. Direct access to its MACADDR property */
  50. $getMac = new Getmac (php_os);
  51. Echo $getMac->macaddr;
  52. ?>
Copy Code


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.