search
HomeBackend DevelopmentPHP ProblemHow to obtain the computer IP, host name, and mac address of all users in php LAN

Introduces in detail the implementation of php to obtain the computer IP, host name, and mac address of all users on the LAN. It is very practical. Friends in need can refer to it

How to obtain the computer IP, host name, and mac address of all users in php LAN

The code is as follows:

<?php
$bIp = gethostbyname($_ENV[&#39;COMPUTERNAME&#39;]); //获取本机的局域网IP
echo "本机IP:",$bIp,"\n";
echo "本机主机名:",gethostbyaddr($bIp),"\n\n\n"; //gethostbyaddr 函数可以根据局域网IP获取主机名
//默认网关IP
list($ipd1,$ipd2,$ipd3) = explode(&#39;.&#39;,$bIp);
$mask = $ipd1 . "." . $ipd2 . "." . $ipd3 ;
exec(&#39;arp -a&#39;,$aIp); //获取局域网中的其他IP
foreach( $aIp as $ipv) {
 if(strpos($ipv,&#39;接口&#39;) !== false) {//一下显示的IP是否是当前局域网中的 而不是其他的类型 可以在cmd下试一下命令
 $bool = false;
 preg_match(&#39;/(?:(?: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)))/&#39;,$ipv,$arr);
 if(strcmp($arr[0],$bIp) == 0) {
  $bool = true;
 }
 } else {
 if($bool) {
  $str = preg_replace(&#39;/\s+/&#39;, &#39;|&#39;, $ipv);
  $sArr = explode(&#39;|&#39;,$str);
  if($sArr[1] == &#39;Internet&#39; || empty($sArr[1])) {
  continue;
  }
  //去除默认网关
  if(strcmp($mask . ".1", $sArr[1]) == 0) {
  continue;
  }
  //去除同网关下255的IP
  if(strcmp($mask . ".255", $sArr[1]) == 0) {
  continue;
  }
  //去除组播IP
  list($cIp) = explode(&#39;.&#39;, $sArr[1]);
  if($cIp >= 224 && $cIp <= 239) {
  continue;
  }
  echo "IP地址:|",$sArr[1],"|\n";
  echo "MAC地址:",$sArr[2],"\n";
  echo "主机名:",gethostbyaddr($sArr[1]),"\n";
  echo "\n\n";
 }
 }
}

php implements a complete example of obtaining the computer IP, host name, and mac address of all users on the LAN

This program is run in cli mode, on the browser It should also be possible

php has completed the function of getting the user IP in the LAN. The main ones used are the exec function of php and the arp -a command of window

Get the local IP: gethostbyname ($_ENV['COMPUTERNAME']) is different from the previous writing method. Interested friends can continue to study it in depth.

Get the host name function: gethostbyaddr(IPd) This function is also very powerful.

Recommended learning: php video tutorial

The above is the detailed content of How to obtain the computer IP, host name, and mac address of all users in php LAN. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:512笔记. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.