PHP判断所用系统以及浏览器及版本

hyx 2019-07-31 18:01:36 103次 0
  function GetOs() {
            if (!empty($_SERVER['HTTP_USER_AGENT'])) {
                $agent = $_SERVER['HTTP_USER_AGENT'];
                if (preg_match('/win/i', $agent)) {
                    $OS = 'Windows';
                }else if (preg_match('/Android (([0-9_.]{1,3})+)?/i', $agent) ||preg_match('/Android/i', $agent)) {
                    $OS = "Android";
                }else if (preg_match('/iPhone OS (([0-9_.]{1,3})+)/i', $agent)||preg_match('/iPh OS (([0-9_.]{1,3})+)/i', $agent)) {
                    $OS = "iPhone ";
                }elseif (preg_match('/mac/i', $agent)) {
                    $OS = 'MAC';
                } elseif (preg_match('/linux/i', $agent)) {
                    $OS = 'Linux';
                } elseif (preg_match('/unix/i', $agent)) {
                    $OS = 'Unix';
                } elseif (preg_match('/bsd/i', $agent)) {
                    $OS = 'BSD';
                } else {
                    $OS = 'Other';
                }


                $bname = 'Unknown';
                $version = "";

                // Next get the name of the useragent yes seperately and for good reason
                if (preg_match('/MSIE/i', $agent) && !preg_match('/Opera/i', $agent)) {
                    $bname = 'IE';
                    $ub = "MSIE";
                } elseif (preg_match('/Edge/i', $agent)) {
                    $bname = 'Edge';
                    $ub = 'Edge';
                } elseif (preg_match('/Trident\/7.*like Gecko/i', $agent)) {
                    $bname = 'IE';
                    $ub = "rv";
                }elseif(preg_match('/UBrowser/i', $agent)){
                    $bname = 'UC';
                    $ub ="UBrowser";
                }elseif(preg_match('/UCBrowser/i', $agent)){
                    $bname = 'UC';
                    $ub ="UCBrowser";
                } elseif (preg_match('/UCWEB/i', $agent)) {
                    $bname = 'UC';
                    $ub ="UCWEB";
                }
                elseif (preg_match('/QQBrowser/i', $agent)) {
                    $bname = 'qq';
                    $ub ="QQBrowser";
                }
                elseif (preg_match('/MicroMessenger/i', $agent)) {
                    $bname = 'webchat';
                    $ub ="MicroMessenger";
                }
                elseif (preg_match('/Firefox/i', $agent)) {
                    $bname = 'Firefox';
                    $ub = "Firefox";
                } elseif (preg_match('/Chrome/i', $agent)) {
                    $bname = 'Chrome';
                    $ub = "Chrome";
                } elseif (preg_match('/Safari/i', $agent)) {
                    $bname = 'Safari';
                    $ub = "Safari";
                } elseif (preg_match('/Opera/i', $agent)) {
                    $bname = 'Opera';
                    $ub = "Opera";
                } elseif (preg_match('/Netscape/i', $agent)) {
                    $bname = 'Netscape';
                    $ub = "Netscape";
                }


                // finally get the correct version number
                $known = array('Version', $ub, 'other');
                $pattern = '#(?<browser>' . join('|', $known) . ')[/: ]+(?<version>[0-9.|a-zA-Z.]*)#';
                if (!preg_match_all($pattern, $agent, $matches)) {
                    // we have no matching number just continue
                }
                // see how many we have
                $i = count($matches['browser']);
                if ($i != 1) {
                    //we will have two since we are not using 'other' argument yet
                    //see if version is before or after the name
                    if (strripos($agent, "Version") < strripos($agent, $ub)) {
                        $version = $matches['version'][0];
                    } else {
                        $version = $matches['version'][1];
                    }
                } else {
                    $version = $matches['version'][0];
                }



                return $OS."&nbsp;&nbsp;".$bname.'/'.$version;
            } else {
                return "获取失败!";
            }


        }

发表评论

注:*为必填

回复 的评论
*
选择
*
*