Home > Article > Backend Development > PHP gets browser version and operating system version
PHP Get the browser version and operating system version
I accidentally searched for using php to intercept the browser and operating system information in ua. I found it troublesome, so I wrote a
Php code myself
$user_agent = $_SERVER['HTTP_USER_AGENT']; // 先取得UA // echo $user_agent; const REG_ALL = '/(Opera|OPR|Firefox|Chrome|Safari|MSIE|Navigator).(\d{1,3}\.\d{0,2})/i'; // 此处可以加入想要捕获的浏览器标识 preg_match(REG_ALL, $user_agent, $matches); // 这里查下PHP文档 /** 大致返回结果像这样 array (size=3) 0 => string 'Firefox/39.0' (length=12) 1 => string 'Firefox' (length=7) 2 => string '39.0' (length=4) */ const REG_OS = '/(Windows NT|Win |Linux|Unix|Sun|Mac).(\d{1,3}\.\d{0,2})/i'; // 这里仅做了测试项目 preg_match(REG_OS, $user_agent, $matches); // 操作系统如是乎 /* 返回值类似这样似的,自己再做解析,6.1:Win7 5.1:XP 什么的 array (size=3) 0 => string 'Windows NT 6.1' (length=14) 1 => string 'Windows NT' (length=10) 2 => string '6.1' (length=3) */ die();
That’s it, Most browsers can match it. The operating system is more complicated, but it is generally sufficient in a Windows environment.