php获取计算机硬件信息
2020-07-28 14:04:07 来源:admin 点击:933
<?php
header("Content-Type:text/html;charset=utf-8");
class GetmacAddr{
var $result = array(); // 返回带有MAC地址的字串数组
var $macAddr;
/*构造*/
function __construct($osType){
switch ( strtolower($osType) ){
case "unix": break;
case "solaris": break;
case "aix": break;
case "linux": {
$this->for_linux_os();
}break;
default: {
$this->for_windows_os();
}break;
}
$temp_array = array();
foreach($this->result 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->macAddr = $temp_array[0];
break;
}
}
unset($temp_array);
return $this->macAddr;
}
/*linux系统中获取方法*/
function for_linux_os(){
@exec("ifconfig -a", $this->result);
return $this->result;
}
/*win系统中的获取方法*/
function for_windows_os(){
@exec("ipconfig /all", $this->result);
if ( $this->result ) {
return $this->result;
} else {
$ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
if(is_file($ipconfig)) {
@exec($ipconfig." /all", $this->result);
} else {
@exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->result);
return $this->result;
}
}
}
/*获取ip方法*/
function getip() {
static $ip = '';
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
$ip = $_SERVER['HTTP_CDN_SRC_IP'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) {
if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
$ip = $xip;
break;
}
}
}
return $ip;
}
//获取CPU序列号
function getCpuSN()
{
$return_arry = array();
@exec("wmic cpu get processorid", $return_arry);
$cpu_sn = $return_arry[1];
return $cpu_sn;
}
//获取主板序列号
function getBaseboardSN()
{
$return_arry = array();
@exec("wmic baseboard get serialnumber", $return_arry);
$baseboard_sn = $return_arry[1];
$baseboard_sn = str_replace("-", "", $baseboard_sn);//去除字符串中的字符“-”
return $baseboard_sn;
}
}
$hw_addr = new GetmacAddr(PHP_OS);
$mac_addr = $hw_addr->macAddr;
$cpu_sn = $hw_addr->getCpuSN();
$baseboard_sn = $hw_addr->getBaseboardSN();
echo 'MAC地址:'.$mac_addr.'<br>';
echo 'CPU序列号::'.$cpu_sn.'<br>';
echo '主板地址::'.$baseboard_sn.'<br>';