Home >Backend Development >PHP Tutorial >How Can I Retrieve Client IP and MAC Addresses in PHP?
Getting Server Information
You can retrieve the server's IP address from $_SERVER['SERVER_ADDR']. To obtain its MAC address, parse the output of netstat -ie (Linux) or ipconfig /all (Windows).
Obtaining Client Data
The client's IP address is readily available in $_SERVER['REMOTE_ADDR']. However, accessing the MAC address presents a challenge.
Client MAC Address (LAN Only)
If the client and server reside on the same Ethernet segment, you can determine the client's MAC address by parsing the output of arp -n (Linux) or arp -a (Windows).
<?php $ipAddress = $_SERVER['REMOTE_ADDR']; $macAddr = false; $arp = `arp -a $ipAddress`; $lines = explode("\n", $arp); foreach ($lines as $line) { $cols = preg_split('/\s+/', trim($line)); if ($cols[0] == $ipAddress) { $macAddr = $cols[1]; } } ?>
No LAN? No Access to MAC Address
Unfortunately, retrieving the client's MAC address is not feasible if it's located outside the local network, unless the client explicitly provides the information through other means.
The above is the detailed content of How Can I Retrieve Client IP and MAC Addresses in PHP?. For more information, please follow other related articles on the PHP Chinese website!