edgarmtze
edgarmtze

Reputation: 25038

Detect ie10, < ie10 and other browsers in php

I have this code to detect if user is using IE Browser, however I would like to detect if it is ie 10 or a version below ie 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 
    } 

    ?>

so is it correct?

 <?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 
        } 

        ?>

Upvotes: 4

Views: 10587

Answers (3)

Watts Epherson
Watts Epherson

Reputation: 783

You can also check on for the Trident string:

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

So for SVG animation checking I can do:

$IE = preg_match('/Trident\//i',$_SERVER['HTTP_USER_AGENT']);

All the references you need are here:-

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

Upvotes: 0

sergserg
sergserg

Reputation: 22224

You can easily check for IE in the HTTP_USER_AGENT server variable.

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

If you want to specifically target IE10, you can use:

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

Upvotes: 3

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

This might help you:

<?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>>

Edit: Break into 3 cases:

<?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>>

Upvotes: 13

Related Questions