(二)PHP解析HTTP_USER_AGENT 获取客户端操作系统

本文介绍了一种通过分析HTTP_USER_AGENT字符串来识别客户端操作系统、版本及设备类型的方法。适用于Windows、Linux、Unix、iOS、Android等系统,并区分如Windows Vista、Windows 10、MacOSX等具体版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取客户端操作系统 

可以通过内核版本判断

 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的博客-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值