Php obtains the IP address, host name, and mac address of all LAN users.

Source: Internet
Author: User
This article describes how to use php to obtain the IP address, host name, and mac address of all users in the LAN, if you need a php instance, you can refer to the php instance described in this article to obtain the computer IP address, host name, and mac address of all users in the LAN. It provides some reference for php programmers. The complete code is as follows:

<? Php $ bIp = gethostbyname ($ _ ENV ['computername']); // Obtain the local LAN IPecho "local IP:", $ bIp, "\ n "; echo "local host name:", gethostbyaddr ($ bIp), "\ n "; // The gethostbyaddr function can obtain the host name based on the lan ip address // Default Gateway IPlist ($ ipd1, $ ipd2, $ ipd3) = explode ('. ', $ bIp); $ mask = $ ipd1. ". ". $ ipd2. ". ". $ ipd3; exec ('arp-A', $ aIp); // obtain other IPforeach ($ aIp as $ ipv) {if (strpos ($ ipv, 'interface ')! = False) {// check whether the displayed IP address is in the current LAN rather than other types. you can try the command $ bool = false in cmd; 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 the default gateway if (strcmp ($ mask. ". 1 ", $ sArr [1]) = 0) {continue;} // remove the 255 IP address if (strcmp ($ mask. ". 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 ";}}}

This program runs in cli mode and can be used in a browser.
Php obtains the user ip address in the LAN. it mainly uses the exec function of php and the arp-a command of the window.
Get the local IP address: gethostbyname ($ _ ENV ['computername']) is different from the previous method. if you are interested, you can continue to study it in depth.
The gethostbyaddr (IPd) function is also powerful.

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.