Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Detecting IE8 and lower with PHP

Detecting IE8 and lower with PHP

Just yesterday I got myself an Xbox One (There may be a review on this coming soon). I tried out Internet Explorer on it, which I found out was Internet Explorer 10.

I also discovered that my PHP script to detect Internet Explorer 8 accepted Internet Explorer 10 as being an older version than Internet Explorer 8. This is a simple mistake to make but it's also incredibly easy to fix.

Here was what I had:

PHP
preg_match("/.* MSIE [1-8] .*/i", $userAgent)

And here is a working solution, to detect all browsers less than IE8:

PHP
preg_match("/.* MSIE [1-8].[0-9]?; .*/i", $userAgent)

And the reason for this happening is down to the fact this only checks the first number of the version, not the second, so IE10 would be recognised as IE1. I also put in, just for the sake of it, a check for a dot (.) and a check for a minor version number ([0-9]) and a semi-colon at the end. At the beginning and end of the regular expression match are any symbols.

internet
explorer
ie
8
version
1
2
3
4
5
6
7
detect
php
user
agent
string
find
get
check
Comments
Powered by DASH 2.0