php获取网卡的MAC地址支持WIN/LINUX系统

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
/**
获取网卡的MAC地址原码;目前支持WIN/LINUX系统
获取机器网卡的物理(MAC)地址
**/

class GetMacAddr{

var $return_array = array(); // 返回带有MAC地址的字串数组
var $mac_addr;

function GetMacAddr($os_type){
switch ( strtolower($os_type) ){
case "linux":
$this->forLinux();
break;
case "solaris":
break;
case "unix":
break;
case "aix":
break;
default:
$this->forWindows();
break;

}

$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];
break;
}

}
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(PHP_OS);
echo $mac->mac_addr; //这里是机器的真实MAC地址,请注释掉
?>

相关文章

数组与类使用PHP的可变变量名需要的注意的问题

有时候可变的变量名会给编程带来很大的方便。也就是说变量名可以被动态的命名和使用。通常变量通过下面这样的语句来命名 :$a = 'hello';可变变量名指的是使用一个变量的值作为这个变量...

PHP中使用foreach和引用导致程序BUG的问题介绍

复制代码 代码如下: $a = array(1, 2); $b = array(11, 12); foreach($a as &$r){ } foreach($b as $r){ } e...

PHP使用debug_backtrace方法跟踪调试代码调用详解

本文实例讲述了PHP使用debug_backtrace方法跟踪调试代码调用。分享给大家供大家参考,具体如下: 在开发过程中,例如要修改别人开发的代码或调试出问题的代码,需要对代码流程一步...

php中批量修改文件后缀名的函数代码

复制代码 代码如下:<?php function foreachDir($path){ $handle=opendir($path); if($handle){ while (fa...

php中常见的sql攻击正则表达式汇总

本文实例讲述了php中常见的sql攻击正则表达式。分享给大家供大家参考。具体分析如下: 我们都已经知道,在MYSQL 5+中 information_schema库中存储了所有的 库名,...