Home >Backend Development >PHP Tutorial >How Can I Retrieve Client IP and MAC Addresses in PHP?

How Can I Retrieve Client IP and MAC Addresses in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-12-28 16:31:09773browse

How Can I Retrieve Client IP and MAC Addresses in PHP?

PHP: Retrieving Client MAC and IP Address

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!

Statement:
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