php Gets Address Support for Server side mac and Client side mac WIN and LINUX

  • 2021-06-28 11:30:23
  • OfStack

Get Server mac
 
<?php 
/** 
 Get Network Card's MAC Address source code;Current support WIN/LINUX system  
 Get the physical of the machine network card ( MAC ) Address  
**/ 
class GetmacAddr{ 
var $result = array(); //  Return with MAC String array of addresses  
var $macAddr; 
/* structure */ 
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 System acquisition method */ 
function for_linux_os(){ 
@exec("ifconfig -a", $this->result); 
return $this->result; 
} 
/*win Acquisition methods in the system */ 
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; 
} 
} 
} 
} 
?> 

Get the client mac address:
 
@exec("arp -a",$array); // implement arp -a Command, result to array $array in  
foreach($array as $value){ 
// Place matching results in an array $mac_array 
if(strpos($value,$_SERVER["REMOTE_ADDR"]) && preg_match("/(:?[0-9A-F]{2}[:-]){5}[0-9A-F]{2}/i",$value,$mac_array)){ 
$mac = $mac_array[0]; 
break; 
} 
} 
echo $mac; 

Note: mac acquired by the client cannot be tested locally and can only be output if accessed by another computer

Related articles: