In Web development, it is essential to customize web pages according to the operating system used by the user. PHP can identify the operating system the script is running on and the browser the user is using. This article will introduce how to identify the operating system used by the user in PHP and how to distinguish between Android and iOS.
The following two methods are given:
Method 1: Use the HTTP_USER_AGENT variable
HTTP_USER_AGENT is a request header sent by the browser to the Web server, which contains the user agent character string. This string generally contains the browser name, browser version, operating system name, and operating system version. In PHP, you can use the following code to get the value of this variable:
$user_agent = $_SERVER['HTTP_USER_AGENT'];
After getting the value, we usually need to find the user agent character The information contained in the string. To determine the user's operating system, the following are common user agent strings:
Windows
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 58.0.3029.96 Safari/537.36
macOS
Mozilla/5.0 (Macintosh; Intel Mac OS /5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile Safari/537.36
iOS
Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS and its version, the following is the sample code:
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/iphone|ipod|ipad/i', $user_agent) ) {
// iOS if(preg_match('/OS\s(\d+)_(\d+)/i', $user_agent, $matches)) { $os_version = $matches[1] . '.' . $matches[2]; } echo 'iOS ' . $os_version;
} else if (preg_match('/android/i', $user_agent)) {
// Android if(preg_match('/Android\s(\d+(\.\d+)?)/i', $user_agent, $matches)) { $os_version = $matches[1]; } echo 'Android ' . $os_version;
} else if (preg_match('/windows nt/i', $user_agent )) {
// Windows if(preg_match('/Windows NT (\d+(\.\d+)?)/i', $user_agent, $matches)) { $os_version = $matches[1]; } echo 'Windows ' . $os_version;
} else {
echo 'Unknown OS';
}
Method 2: Use PHP’s get_browser function
The get_browser function can return the browser used by the user and operating system related information. However, this function relies on the browscap.ini file installed by PHP and data matching the specified user agent information.
The following is sample code:
$browser_info = get_browser(null, true);
$os = $browser_info['platform'];if (preg_match ('/iphone|ipod|ipad/i', $_SERVER['HTTP_USER_AGENT'])) {echo 'iOS ' . $os;
echo 'Android ' . $os;
} else {
echo 'Unknown OS';
}
It should be noted that the PHP browscap.ini file needs to be downloaded separately, and its download link is https://browscap .org/stream?q=Full_PHP_BrowsCapINI
Conclusion
Both methods can determine the user's operating system on PHP, you just need to choose the method you like. No matter which method you choose, it can well meet the needs of user operating systems in web development.
The above is the detailed content of How to use php to determine whether the device is Android or iOS. For more information, please follow other related articles on the PHP Chinese website!