如何在 PHP 中执行浏览器检测 - IE(版本)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21859268/
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 04:28:51  来源:igfitidea点击:

How to perform Browser detection - IE (versions) in PHP

phpinternet-explorer

提问by Joselito

I found this code to detect the browser via php:

我发现这段代码可以通过 php 检测浏览器:

<?php
$msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false;
$firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
$safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
$chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
?>

<?php
//Firefox
if ($firefox) {
echo 'you are using Firefox!';
echo '<br />';
}

// Safari or Chrome. Both use the same engine - webkit
if ($safari || $chrome) { 
echo 'you are using a webkit powered browser';
echo '<br />';
}

// IE
if ($msie) {
echo '<br>you are using Internet Explorer<br>';
echo '<br />';
}?>

Source

来源

But the code does not include the possible versions of IE. Did something like this:

但代码不包括可能的 IE 版本。做了这样的事情:

// IE7
if ($msie7) {
echo '<br>you are using Internet Explorer 7<br>';
echo '<br />';
}

Could someone help me with this? Wanted to improve the code including support IE ??versions.

有人可以帮我解决这个问题吗?想要改进代码,包括支持 IE 版本。

回答by Ferrakkem Bhuiyan

Try this:

尝试这个:

<?php
    $ie6 = (ereg("MSIE 6", $_SERVER["HTTP_USER_AGENT"])) ? true : false;
    $ie7 = (ereg("MSIE 7", $_SERVER["HTTP_USER_AGENT"])) ? true : false;
    $ie8 = (ereg("MSIE 8", $_SERVER["HTTP_USER_AGENT"])) ? true : false;

    if ($ie6 || $ie7 || $ie8) {
        // Do fallback stuff that old browsers can do here
        echo "You are using IE";
    } else {
        // Do stuff that real browsers can handle here
    }
?>

回答by Carles Solà

This PHP function works perfectly. But I do not know how to make PHP discriminate between the different versions of "MSIE"

这个 PHP 函数完美运行。但我不知道如何让 PHP 区分不同版本的“MSIE”

    <?php 
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ){
        header( 'Location: http://www.domain.com' ) ;
    }
?>

Very Thanks

很感谢

回答by MonkeyZeus

There is no reliable way for PHP to know, nor care, which browser is calling upon it. By design, server-side languages have no reason to consider what browser you are using before executing code. $_SERVER['HTTP_USER_AGENT']is a shim at best and changes so frequently that you are bound to have it screw you over sooner rather than later.

PHP 没有可靠的方法来知道或关心哪个浏览器正在调用它。按照设计,服务器端语言在执行代码之前没有理由考虑您使用的是什么浏览器。$_SERVER['HTTP_USER_AGENT']充其量只是一个垫片,而且变化如此频繁,以至于您迟早会被它搞砸。

Your only goal as a web developer should be to write both CSS and JS code which is either cross-browser compatible or use a conditional <!--[if IE 8]><![endif]-->shim which has apparently been dropped by IE11 anyways...

作为 Web 开发人员,您唯一的目标应该是编写跨浏览器兼容的 CSS 和 JS 代码,或者使用<!--[if IE 8]><![endif]-->显然已被 IE11 删除的条件垫片...

In JS you need to check if a function exists before calling it or figure out if the JS engine which supports it is available within your browser target scope.

在 JS 中,您需要在调用一个函数之前检查它是否存在,或者确定支持它的 JS 引擎是否在您的浏览器目标范围内可用。

With that being said, yes there are libraries out there which attempt to decipher $_SERVER['HTTP_USER_AGENT']but the information source is flawed in the first place so the library is useless from the moment you try using it.

话虽如此,是的,有些图书馆试图破译,$_SERVER['HTTP_USER_AGENT']但信息源首先存在缺陷,因此从您尝试使用它的那一刻起,图书馆就毫无用处。

Here is some cross-browser CSS:

这是一些跨浏​​览器的 CSS:

.someClass{
    -webkit-box-shadow: 0 0 5px black;
    -moz-box-shadow:    0 0 5px black;
    box-shadow:         0 0 5px black;

    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);

    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;

    /* IE supports nothing above? Too bad, it will ignore it */
}