获取操作系统信息
在 PHP 中,您可以使用 get_browser() 函数来检索有关用户的浏览器和操作系统的信息。该函数返回一个包含以下信息的数组:
要检索用户的操作系统信息,您可以使用get_browser() 函数返回的数组的平台元素。例如:
$browser = get_browser(); echo $browser['platform'];
输出:
Windows
示例代码:
<?php // Get user agent from header $userAgent = $_SERVER['HTTP_USER_AGENT']; // Sniff the operating system $os = null; if (preg_match('/windows nt 10/i', $userAgent)) { $os = 'Windows 10'; } elseif (preg_match('/windows nt 6.3/i', $userAgent)) { $os = 'Windows 8.1'; } elseif (preg_match('/windows nt 6.2/i', $userAgent)) { $os = 'Windows 8'; } elseif (preg_match('/windows nt 6.1/i', $userAgent)) { $os = 'Windows 7'; } elseif (preg_match('/windows nt 6.0/i', $userAgent)) { $os = 'Windows Vista'; } elseif (preg_match('/windows nt 5.2/i', $userAgent)) { $os = 'Windows XP x64'; } elseif (preg_match('/windows nt 5.1/i', $userAgent)) { $os = 'Windows XP'; } elseif (preg_match('/windows xp/i', $userAgent)) { $os = 'Windows XP'; } elseif (preg_match('/windows nt 5.0/i', $userAgent)) { $os = 'Windows 2000'; } elseif (preg_match('/windows me/i', $userAgent)) { $os = 'Windows ME'; } elseif (preg_match('/win98/i', $userAgent)) { $os = 'Windows 98'; } elseif (preg_match('/win95/i', $userAgent)) { $os = 'Windows 95'; } elseif (preg_match('/win16/i', $userAgent)) { $os = 'Windows 3.11'; } elseif (preg_match('/macintosh|mac os x/i', $userAgent)) { $os = 'Mac OS X'; } elseif (preg_match('/mac_powerpc/i', $userAgent)) { $os = 'Mac OS 9'; } elseif (preg_match('/linux/i', $userAgent)) { $os = 'Linux'; } elseif (preg_match('/ubuntu/i', $userAgent)) { $os = 'Ubuntu'; } elseif (preg_match('/iphone/i', $userAgent)) { $os = 'iPhone'; } elseif (preg_match('/ipod/i', $userAgent)) { $os = 'iPod'; } elseif (preg_match('/ipad/i', $userAgent)) { $os = 'iPad'; } elseif (preg_match('/android/i', $userAgent)) { $os = 'Android'; } elseif (preg_match('/blackberry/i', $userAgent)) { $os = 'BlackBerry'; } elseif (preg_match('/webos/i', $userAgent)) { $os = 'Mobile'; } else { $os = 'Unknown'; } // Print the operating system echo $os; ?>
以上是如何使用PHP检测用户的操作系统?的详细内容。更多信息请关注PHP中文网其他相关文章!