<?
//php获取ip的算法
if(getenv('HTTP_CLIENT_IP')) {
$onlineip = getenv('HTTP_CLIENT_IP')
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR')
} elseif(getenv('REMOTE_ADDR')) {
$onlineip = getenv('REMOTE_ADDR')
} else {
$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR']
}
exec ("ping $onlineip")
$macaddress=exec ("arp -a | find /i".' "'.$onlineip.'"')
$computername=exec ('nbtstat -A'.' '.$onlineip.' | find /i "20"')
$MacName=(preg_split("/\s+/", trim($macaddress)))
$UserName=(preg_split("/\s+/", trim($computername)))
echo "计算机名:".$UserName[0]." --- "."IP 地址:".$MacName[0]." --- "."MAC 地址:".$MacName[1]
?>
class GetMacAddr
{
var $return_array = array()// 返回带有MAC地址的字串数组
var $mac_addr=array()
function GetMacAddr()
{
switch (strtolower(PHP_OS) )
{
case "linux":$this->forLinux()break
case "solaris":break
case "unix":break
case "aix":break
default:$this->forWindows()break
}
//www.51qgj.com
$temp_array = array()
foreach ( $this->return_array as $value )
{
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, $temp_array ) )
{
$this->mac_addr[] = $temp_array[0]
}
}
unset($temp_array)
return $this->mac_addr
}
function forWindows()
{
@exec("ipconfig /all", $this->return_array)
if ( $this->return_array )
return $this->return_array
else{
$ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe"
if ( is_file($ipconfig) )
@exec($ipconfig." /all", $this->return_array)
else
@exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array)
return $this->return_array
}
}
function forLinux()
{
@exec("ifconfig -a", $this->return_array)
return $this->return_array
}
}
$mac = new GetMacAddr()
echo "<pre>"
print_r( $mac->mac_addr)
?>
mac测试过 可能无效
本文所述php实例可以完成获取局域网所有用户的电脑IP和主机名、及mac地址的功能,对于php程序设计人员有一定的参考借鉴价值。完整代码如下:<?php
$bIp = gethostbyname($_ENV['COMPUTERNAME'])//获取本机的局域网IP
echo "本机IP:",$bIp,"\n"
echo "本机主机名:",gethostbyaddr($bIp),"\n\n\n"//gethostbyaddr 函数可以根据局域网IP获取主机名
//默认网关IP
list($ipd1,$ipd2,$ipd3) = explode('.',$bIp)
$mask = $ipd1 . "." . $ipd2 . "." . $ipd3
exec('arp -a',$aIp)//获取局域网中的其他IP
foreach( $aIp as $ipv) {
if(strpos($ipv,'接口') !== false) {//一下显示的IP是否是当前局域网中的 而不是其他的类型 可以在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
}
//去除默认网关
if(strcmp($mask . ".1", $sArr[1]) == 0) {
continue
}
//去除同网关下255的IP
if(strcmp($mask . ".255", $sArr[1]) == 0) {
continue
}
//去除组播IP
list($cIp) = explode('.', $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"
}
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)