PHP implementation to obtain the LAN all users of the computer IP and host name, and MAC address complete instance _php skills

Source: Internet
Author: User
Tags explode strcmp

The PHP instance described in this article can accomplish the function of acquiring the computer IP and host name and MAC address of all the users in the LAN, and has some reference value for the PHP program designers. The complete code is as follows:

<?php $bIp = gethostbyname ($_env[' COMPUTERNAME '));
Get local LAN IP echo "native IP:", $bIp, "\ n"; echo "Native host name:", gethostbyaddr ($bIp), "\n\n\n";
The GETHOSTBYADDR function can get host name//default gateway IP list ($IPD 1, $IPD 2, $IPD 3) = Explode ('. ', $bIp) based on LAN IP $mask = $IPD 1. "." . $IPD 2. "." .
$IPD 3; EXEC (' arp-a ', $aIp); Obtain other IP foreach ($aIp as $IPV) {if (Strpos ($IPV, ' interface ')!== false) {//The display of IP is the current local area network instead of other types can try the command under CMD $bool
 = false; Preg_match ('/(?:(? : 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d))] \.) {3} (?: 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d))]
 /', $IPV, $arr);
 if (strcmp ($arr [0], $bIp) = = 0) {$bool = true;
  } else {if ($bool) {$str = preg_replace ('/\s+/', ' | ', $IPV);
  $SARR = Explode (' | ', $STR);
  if ($sArr [1] = = ' Internet ' | | empty ($SARR [1])) {continue; }//Remove default gateway if (strcmp $mask.
  ". 1", $SARR [1]) = = 0) {continue; //Remove the IP if (strcmp ($mask) under the same gateway 255.
  ". 255", $SARR [1]) = = 0) {continue;
  //Remove multicast IP list ($cIp) = Explode ('. ', $SARR [1]); if ($cIp >= 224 && $cIp <= 239) {continue;
  echo "IP address: |", $SARR [1], "|\n";
  echo "MAC Address:", $SARR [2], "\ n";
  echo "Host name:", gethostbyaddr ($SARR [1]), "\ n";
 echo "\ n"; }
 }
}

The program is running in CLI mode and should also be available on the browser
PHP to get the User IP function on the LAN is done, the main use is the EXEC function of PHP and window arp-a  command
The acquisition of this machine Ip:gethostbyname ($_env[' COMPUTERNAME ') is different from the previous writing, interested friends can continue to further study.
Get host name function: gethostbyaddr (IPd) This function is also powerful.

Related Article

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.