PHP obtains the addresses of server mac and client mac and supports WIN/LINUX_PHP tutorial

WBOY
Release: 2016-07-13 10:29:38
Original
1060 people have browsed it

Get the server mac

Copy code The code is as follows:

/**
Get the original MAC address of the network card; currently supports WIN/LINUX system
Get the physical (MAC) address of the machine's network card
**/
class GetmacAddr{
var $result = array(); // Returns a string array with MAC address
var $macAddr;
/*Construction*/
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;
}
/*Getting method in linux system*/
function for_linux_os(){
@exec("ifconfig -a", $this->result);
return $this->result;
}
/*Getting method in win system*/
function for_windows_os(){
@exec("ipconfig /all", $this->result);
if ( $this->result ) {
return $this->result;
} else {
$ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";
if(is_file($ipconfig)) {
@exec($ipconfig." /all", $this->result );
} else {
@exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->result);
return $this->result;
}
}
}
}
?>

Get the client mac address:
Copy code The code is as follows:

@exec("arp -a",$array); //Execute the arp -a command and put the result into the array $array
foreach ($array as $value){
//The matching results are placed in the 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: The mac obtained by the client cannot be tested on this machine and can only be output by accessing it from another computer.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/770587.htmlTechArticleGet the server mac. Copy the code as follows: ?php /**Obtain the original MAC address of the network card; currently supports WIN/LINUX system. Obtain the physical (MAC) address of the machine's network card **/ class GetmacAddr{...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!