任何用于检测具有版本和操作系统的浏览器的 php 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:13:07  来源:igfitidea点击:

Any php code to detect the browser with version and operating system?

php

提问by Ryan

I tried to search in google but cannot find a complete solution (i only find something detects only the browser's type like firefox, opera) .

我试图在 google 中搜索,但找不到完整的解决方案(我只发现某些东西只能检测浏览器的类型,例如 firefox、opera)。

i want a php class or code to check the user's Browser including the versionand also the operating system.

我想要一个 php 类或代码来检查用户的浏览器,包括版本和操作系统。

Thanks

谢谢

回答by goker.cebeci

a simple way for example:

一个简单的方法例如:

function browser() {
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $browser = 'chromium';
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $browser = 'chrome';
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $browser = 'safari';
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $browser = 'opera';
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $browser = 'msie';
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $browser = 'mozilla';

    preg_match('/('.$browser.')[ \/]([\w]+)/', $ua, $version);

    return array($browser,$version[2], 'name'=>$browser,'version'=>$version[2]);
}

its return like

它的回报就像

chromium 15
chrome 16
opera 9

回答by AlexV

I used the techpatterns.comone and they don't always update it and it use procedural code which feel dated...

我使用了techpatterns.com一个,他们并不总是更新它,它使用感觉过时的程序代码......

The Wolfcast BrowserDetection PHP classis updated and use an Object-Oriented way to do it:

Wolfcast BrowserDetection PHP类被更新,并使用面向对象的方式做到这一点:

You use it this way:

你这样使用它:

$browser = new BrowserDetection();
echo 'You are using ', $browser->getBrowser(), ' version ', $browser->getVersion();

Another example:

另一个例子:

$browser = new BrowserDetection();
if ($browser->getBrowser() == BrowserDetection::BROWSER_FIREFOX && $browser->compareVersions($browser->getVersion(), '5.0.1') !== 1) {
    echo 'You have FireFox version 5.0.1 or greater. ';
}

回答by Jay Zeng

PHP actually has a native method for detecting browser info, called get-browser

PHP 实际上有一个用于检测浏览器信息的本地方法,称为get-browser

Directly copied from PHP documentation:

直接从 PHP 文档复制:

<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
print_r($browser);
?>

The above example will output something similar to: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

上面的示例将输出类似于: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
)


回答by pedro

get_browser()gives you browser version and operating system

get_browser()为您提供浏览器版本和操作系统

$browser = get_browser();

foreach ($browser as $name => $value) {
    echo "$name $value\n";
}

output:
browser_name_pattern:</b> Mozilla/4\.5.*
parent: Netscape 4.0
platform: Linux
...