PHP Get server-side Mac and client MAC address support win/linux_php tips

Source: Internet
Author: User
Tags mongodb postgresql redis
Get Server Mac
code as follows:

<?php
/**
Obtain the MAC address origin of the NIC; currently supports Win/linux system
Get the physical (MAC) address of the machine's network card
**/
Class getmacaddr{
var $result = array (); Returns an array of strings with a MAC address
var $macAddr;
/* Construction */
function __construct ($osType) {
Switch (Strtolower ($osType)) {
Case "UNIX": break;
Case "Solaris": Break;
Case "Aix": a 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;
}
The method of obtaining in/*linux system * *
function For_linux_os () {
@exec ("Ifconfig-a", $this->result);
return $this->result;
}
The method of obtaining in/*win 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;
}
}
}
}
?>


To obtain the client MAC address:
code as follows:

@exec ("Arp-a", $array); Executes the ARP-A command, and the result is placed in the array $array
foreach ($array as $value) {
Match Results to 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 can not be tested on the computer and can only be accessed with other computers

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.