How does the operating system of the server affect the ability to retrieve MAC addresses using PHP?

The operating system of the server can affect the ability to retrieve MAC addresses using PHP because the function used to retrieve MAC addresses may not be available on all operating systems. To solve this issue, you can use a combination of PHP functions to retrieve the MAC address based on the operating system.

function getMacAddress() {
    $os = PHP_OS;
    if (strpos($os, "WIN") !== false) {
        exec("ipconfig /all", $output);
        foreach ($output as $line) {
            if (preg_match("/Physical Address/", $line)) {
                $mac = explode(":", $line);
                return trim($mac[1]);
            }
        }
    } else {
        $mac = shell_exec("/sbin/ifconfig | grep -Po 'HWaddr \K.*'");
        return trim($mac);
    }
}

echo getMacAddress();