获取客户端操作系统
可以通过内核版本判断
Windows NT 4.0 = NT 4.0
Windows 2000 = NT 5.0
Windows XP = NT 5.1
Windows Vista = NT 6.0
Windows 7 = NT 6.1
Windows 8 = NT 6.2
Windows 8.1 = NT 6.3
Windows 10 = NT 10.0
/**
* 获取客户端操作系统
* @param $agent //$_SERVER['HTTP_USER_AGENT']
* @return array[os] 操作系统名称
* @return array[os_ver] 操作系统版本号
* @return array[equipment] 终端设备类型
*/
function getClientOS($agent = ''){
//window系统
if (stripos($agent, 'window')) {
$os = 'Windows';
$equipment = '电脑';
if (preg_match('/nt 6.0/i', $agent)) {
$os_ver = 'Vista';
}elseif(preg_match('/nt 10.0/i', $agent)) {
$os_ver = '10';
}elseif(preg_match('/nt 6.3/i', $agent)) {
$os_ver = '8.1';
}elseif(preg_match('/nt 6.2/i', $agent)) {
$os_ver = '8.0';
}elseif(preg_match('/nt 6.1/i', $agent)) {
$os_ver = '7';
}elseif(preg_match('/nt 5.1/i', $agent)) {
$os_ver = 'XP';
}elseif(preg_match('/nt 5/i', $agent)) {
$os_ver = '2000';
}elseif(preg_match('/nt 98/i', $agent)) {
$os_ver = '98';
}elseif(preg_match('/nt/i', $agent)) {
$os_ver = 'nt';
}else{
$os_ver = '';
}
if (preg_match('/x64/i', $agent)) {
$os .= '(x64)';
}elseif(preg_match('/x32/i', $agent)){
$os .= '(x32)';
}
}
elseif(stripos($agent, 'linux')) {
if (stripos($agent, 'android')) {
preg_match('/android\s([\d\.]+)/i', $agent, $match);
$os = 'Android';
$equipment = 'Mobile phone';
$os_ver = $match[1];
}else{
$os = 'Linux';
}
}
elseif(stripos($agent, 'unix')) {
$os = 'Unix';
}
elseif(preg_match('/iPhone|iPad|iPod/i',$agent)) {
preg_match('/OS\s([0-9_\.]+)/i', $agent, $match);
$os = 'IOS';
$os_ver = str_replace('_','.',$match[1]);
if(preg_match('/iPhone/i',$agent)){
$equipment = 'iPhone';
}elseif(preg_match('/iPad/i',$agent)){
$equipment = 'iPad';
}elseif(preg_match('/iPod/i',$agent)){
$equipment = 'iPod';
}
}
elseif(stripos($agent, 'mac os')) {
preg_match('/Mac OS X\s([0-9_\.]+)/i', $agent, $match);
$os = 'Mac OS X';
$equipment = '电脑';
$os_ver = str_replace('_','.',$match[1]);
}
else {
$os = 'Other';
}
return ['os'=>$os, 'os_ver'=>$os_ver, 'equipment'=>$equipment];
}
这个参考 http://blog.sina.com.cn/s/blog_68b0cad1010169vr.html 这个博客改编,添加win8、win10
eregi =》stristr eregi(此函数php5.3废弃,php7.0删除)
其他链接
(一)通过IP获取客户端相关访问信息 (一)PHP通过IP获取客户端相关访问信息_denglei的博客-优快云博客
(二)获取客户端操作系统 (二)PHP解析HTTP_USER_AGENT 获取客户端操作系统_denglei的博客-优快云博客
(三)获取客户端浏览器以及版本号(三)PHP解析HTTP_USER_AGENT 获取客户端浏览器以及版本号_denglei的博客-优快云博客_php解析useragent
(四)获取客户端手机型号 (四)PHP解析HTTP_USER_AGENT 获取客户端手机型号_denglei的博客-优快云博客
(五)记录IP和客户端访问信息 (五)PHP解析HTTP_USER_AGENT 记录IP和客户端访问信息_denglei的博客-优快云博客