用户画像统计分析之获取访问用户的操作系统-php实用功能记录(六)

获取访问者的操作系统,这一篇也算是通过UA构建用户画像的完结篇了。
获取操作系统分为两个方法,一个是PC的操作系统,一个是WAP的
获取PC操作系统:
function getOS($agent){
$os = false;
if (preg_match('/win/i', $agent) && strpos($agent, '95')){
$os = 'win 95';
}else if(preg_match('/win 9x/i', $agent) && strpos($agent, '4.90')){
$os = 'win ME';
}else if(preg_match('/win/i', $agent) && preg_match('/98/i', $agent)){
$os = 'win 98';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent)){
$os = 'win Vista';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent)){
$os = 'win 7';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent)){
$os = 'win 8';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent)){
$os = 'win 10';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent)){
$os = 'win XP';
}else if(preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent)){
$os = 'win 2000';
}else if(preg_match('/win/i', $agent) && preg_match('/nt/i', $agent)){
$os = 'win NT';
}else if(preg_match('/win/i', $agent) && preg_match('/32/i', $agent)){
$os = 'win 32';
}else if(preg_match('/linux/i', $agent)){
$os = 'Linux';
}else if(preg_match('/unix/i', $agent)){
$os = 'Unix';
}else if(preg_match('/sun/i', $agent) && preg_match('/os/i', $agent)){
$os = 'SunOS';
}else if(preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent)){
$os = 'IBM OS/2';
}else if(preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent)){
$os = 'Macintosh';
}else if(preg_match('/PowerPC/i', $agent)){
$os = 'PowerPC';
}else if(preg_match('/AIX/i', $agent)){
$os = 'AIX';
}else if(preg_match('/HPUX/i', $agent)){
$os = 'HPUX';
}else if(preg_match('/NetBSD/i', $agent)){
$os = 'NetBSD';
}else if(preg_match('/BSD/i', $agent)){
$os = 'BSD';
}else if(preg_match('/OSF1/i', $agent)){
$os = 'OSF1';
}else if(preg_match('/IRIX/i', $agent)){
$os = 'IRIX';
}else if(preg_match('/FreeBSD/i', $agent)){
$os = 'FreeBSD';
}else if(preg_match('/teleport/i', $agent)){
$os = 'teleport';
}else if(preg_match('/flashget/i', $agent)){
$os = 'flashget';
}else if(preg_match('/webzip/i', $agent)){
$os = 'webzip';
}else if(preg_match('/offline/i', $agent)){
$os = 'offline';
}else{
$os = '未知系统';
}
return $os;
}获取WAP操作系统:
function getWap($agent){
if (strpos($agent, 'Android') !== false) {
preg_match("/(?<=Android )[\d\.]{1,}/", $agent, $version);
return 'Android '.$version[0];
} elseif (strpos($agent, 'iPhone') !== false) {
preg_match("/(?<=CPU iPhone OS )[\d\_]{1,}/", $agent, $version);
return 'iPhone '.str_replace('_', '.', $version[0]);
} elseif (strpos($agent, 'iPad') !== false) {
preg_match("/(?<=CPU OS )[\d\_]{1,}/", $agent, $version);
return 'iPad '.str_replace('_', '.', $version[0]);
}else{
return '未知系统';
}
}$agent参数是用户的UA,也就是$_SERVER['HTTP_USER_AGENT']。
UA的其他使用:
《用户画像统计分析之获取用户浏览器-php实用功能记录(四)》
《获取用户手机设备类型用户画像统计分析-php实用功能记录(五)》
其他说明:
由于UA是可以伪造的,所以这些可以作为参考





有 0 位网友评论: