Get IE Browser Version Using PHP

The code below shows you the way to get the version of IE browser using PHP. To do this, we simply look at User-Agent header data ($_SERVER['HTTP_USER_AGENT']) sent by the browser to determine whether the visitors are using IE browser and then extract IE version from it.

 

1. Implementation

Create a php file and define the getIEVersion function as below.

IEBrowser.php
<?php

function getIEVersion() {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];

    // detect IE below 11
    if (strrpos($userAgent, 'MSIE') !== false) {
        $parts = explode('MSIE', $userAgent);
        $version = (int) $parts[1];
        return $version;
    }

    // detect IE 11
    if (strrpos($userAgent, 'Trident/') !== false) {
        $parts = explode('rv:', $userAgent);
        $version = (int) $parts[1];
        return $version;
    }

    // not found
    return null;
}

 

2. Usage and Example

<?php

// Mozilla/4.0 (compatible; MSIE 5.50; Windows 98; SiteKiosk 4.8)
echo getIEVersion(); // IE version: 5

// Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)
echo getIEVersion(); // IE version: 6

// Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)
echo getIEVersion(); // IE version: 7

// Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)
echo getIEVersion(); // IE version: 8

// Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; Media Center PC 6.0; InfoPath.3; MS-RTC LM 8; Zune 4.7)
echo getIEVersion(); // IE version: 9

// Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0
echo getIEVersion(); // IE version: 10

// Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
echo getIEVersion(); // IE version: 11

Leave a Reply

avatar