取得作業系統資訊
在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中文網其他相關文章!