In web development, understanding client (browser, operating system, etc.) information is very important for debugging and adapting to different environments. PHP comes with the get_browser() function, which can be used to identify the user's browser and its characteristics, but its accuracy is affected by the update frequency of browserscap.ini file and sometimes cannot meet all needs.
In order to more accurately identify client version information, we can use it in conjunction with get_browser() by customizing a get_client_version function. The following will explain in detail how to implement it.
First, make sure that browsercap is enabled in your PHP configuration and points to a latest browsercap.ini file.
Configure in php.ini :
browscap = "https://gitbox.net/path/to/browscap.ini"
Of course, it is recommended to download browser.ini to the local server during actual deployment and configure it as a local path to avoid remote access every request.
Here is a simple example of how to implement it in PHP:
<?php
/**
* Get client version information
*
* @return array
*/
function get_client_version() {
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$browserInfo = get_browser(null, true);
$clientData = [
'browser' => $browserInfo['browser'] ?? 'Unknown',
'version' => $browserInfo['version'] ?? 'Unknown',
'platform' => $browserInfo['platform'] ?? 'Unknown',
'user_agent' => $userAgent,
];
// Supplementary testing for certain special circumstances,For example, mobile
if (stripos($userAgent, 'iPhone') !== false) {
$clientData['device'] = 'iPhone';
} elseif (stripos($userAgent, 'Android') !== false) {
$clientData['device'] = 'Android';
} else {
$clientData['device'] = 'Desktop';
}
return $clientData;
}
// Call Example
$clientInfo = get_client_version();
echo '<pre>';
print_r($clientInfo);
echo '</pre>';
?>
Sometimes the information obtained by get_browser() is relatively rough. We can combine regular expressions to perform secondary analysis of User-Agent, such as identifying WeChat browsers, mini programs, Douyin browsers, etc.:
<?php
/**
* More accurate identification of client versions,Include specificAppBuilt-in browser
*
* @return array
*/
function get_client_version() {
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$browserInfo = get_browser(null, true);
$clientData = [
'browser' => $browserInfo['browser'] ?? 'Unknown',
'version' => $browserInfo['version'] ?? 'Unknown',
'platform' => $browserInfo['platform'] ?? 'Unknown',
'user_agent' => $userAgent,
'device' => 'Desktop',
'app' => 'None'
];
if (stripos($userAgent, 'iPhone') !== false) {
$clientData['device'] = 'iPhone';
} elseif (stripos($userAgent, 'Android') !== false) {
$clientData['device'] = 'Android';
}
// Special detectionAppBuilt-in browser
if (stripos($userAgent, 'MicroMessenger') !== false) {
$clientData['app'] = 'WeChat';
} elseif (stripos($userAgent, 'Douyin') !== false) {
$clientData['app'] = 'Douyin';
} elseif (stripos($userAgent, 'miniProgram') !== false) {
$clientData['app'] = 'MiniProgram';
}
return $clientData;
}
// Call Example
$clientInfo = get_client_version();
echo '<pre>';
print_r($clientInfo);
echo '</pre>';
?>
get_browser() depends on browserscap.ini file. Remember to update it regularly, otherwise the recognition result may be outdated.
Users can forge User-Agent, so these recognition methods are only suitable for optimization experiences and not for security verification.
If more complex identification is needed (such as distinguishing between inside and outside browsers, etc.), it can be further enhanced in combination with third-party libraries such as WhichBrowser/Parser .
By combining PHP's get_browser() function with custom get_client_version logic, we can effectively improve the accuracy and flexibility of client recognition. Especially in the mobile Internet environment, it can accurately identify the device and App environment, and can greatly optimize user experience and function adaptation.