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.