chris
chris

Reputation: 36937

PHP Browser Detection IE > 6

Alright, so Im working on some code in my companies repo, and I stumbled across this bit of browser detection in the login checks. That pretty much searches for MSIE specifically, and wants to decline the login if the browser version is <= 6 however. This is a rather flawed concept at the moment. Seeing as the method takes the user agent string and removes everything but the version number, however the version number in this case is a single digit which works great for version 1-9 detection. However with Internet explorer 10, the result is 1, thus breaking the logic and making it useless.

So right now I am stuck trying to keep this logic for the most part the same, but in essence compensating for versions that would need 2 digits, such as 10+

its been a long while since I've done any browser detection thats version specific. So Im hoping someone can either help me tweak this little bit, or point me towards some better / newer functionality I might be able to use in the same manor.

if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $ua = $_SERVER['HTTP_USER_AGENT'];
    if (strstr($ua,'MSIE') && substr($ua,strpos($ua,'MSIE')+5,1) <= 6 ) {
        return $this->renderMessage(
            self::TEXT_LOGIN_FAILED,
            'It seems that your browser is currently unsupported. We apologize for any inconveniences. Please switch to another browser and try again.'
            );
    }
}

Above is what I am currently working with, as you can see its a bit messy, the person who made it originally didn't think to far a head. And Im stuck not wanting to break it, as I want to keep it similar but not if that makes any sense.

Upvotes: 3

Views: 676

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

How about something like this:

$ua_array = explode(' ', $_SERVER['HTTP_USER_AGENT']);
$msie_key = array_search('MSIE', $ua_array);
if($msie_key !== false) { // you found MSIE browser
    $msie_version_key = $msie_key + 1;
    $msie_version = intval($ua_array[$msie_version_key]);
    if ($msie_version <= 6) {
        // reject user
    }
}

Upvotes: 3

Related Questions