检测ie10,< ie10和php中的其他浏览器

时间:2013-05-10 03:56:35

标签: php html internet-explorer

我有这个代码来检测用户是否正在使用IE浏览器,但是我想检测它是否是10或者是低于10的版本

<?php
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    if(preg_match('/MSIE/i',$u_agent)){
       //do something
       //HOW TO KNOW IS IE 10
       // HOW TO KNOW BROWSER VERSION IS LESS THAN IE 10?
    }else{
       //hope users would always use other browser than IE 
    } 

    ?>

这是正确的吗?

 <?php
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        //IE
        if(preg_match('/MSIE/i',$u_agent)){

           //IE 10
           if(preg_match('/msie 10/i', $_SERVER['HTTP_USER_AGENT'])) {
                     //  DO IE10.
           // < IE 10
           }else{
                     //  DO < IE10.
           } 

        }else{
           //OTHER BROWSERS 
           //hope users would always use other browser than IE 
        } 

        ?>

3 个答案:

答案 0 :(得分:13)

这可能会对您有所帮助:

<?php 
//echo $_SERVER['HTTP_USER_AGENT'];

 if(preg_match('/(?i)msie [10]/',$_SERVER['HTTP_USER_AGENT']))
{
    // if IE = 10
   echo "version is IE 10"; //rest of your code
}
else
{
    // if not 10
     echo "version is not 10"; //rest of your code
}
 ?>

Demo Here>>

修改 分为3个案例:

<?php 
//echo $_SERVER['HTTP_USER_AGENT'];

 if(preg_match('/(?i)msie [1-9]/',$_SERVER['HTTP_USER_AGENT']))
{
    // if IE <= 10
   echo "version is less than 10"; //rest of your code
} else  if(preg_match('/(?i)msie [10]/',$_SERVER['HTTP_USER_AGENT']))
{
    // if IE = 10
   echo "version is IE 10"; //rest of your code
}
else
{
    // if not 10
     echo " other browser"; //rest of your code

}
 ?>

Demo Here>>

答案 1 :(得分:3)

您可以在HTTP_USER_AGENT服务器变量中轻松检查IE。

if(preg_match('/msie [2-10]/i', $_SERVER['HTTP_USER_AGENT'])) {
    // This user is using IE2 - IE10.
} else {
    // Using something else.
}

如果你想专门定位IE10,你可以使用:

if(preg_match('/msie 10/i', $_SERVER['HTTP_USER_AGENT'])) {
    // This user is using IE10.
} else {
    // Using something else.
}

答案 2 :(得分:0)

您还可以检查Trident字符串:

6

因此,对于SVG动画检查,我可以这样做:

Trident/4* = Internet Explorer 8
Trident/5* = Internet Explorer 9
Trident/6* = Internet Explorer 10
Trident/7* = Internet Explorer 11
Edge/* = Edge

您需要的所有参考资料都在这里:-

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537503(v=vs.85)