Wednesday 8 April 2009

IE8 and browser detection

Do you develop websites of your own? If so you may use browser detection methods to find out what the browser used and tweak your website to look nice in other browsers.

Finally moving to IE8 and bypassing version 7 I found that the browser is not detected using PHP. In IE 7 I could use the code:

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7') )
{
$browser = 'ie7';
}

This works for 6 and 7 by changing the number. However for in PHP IE8 reports:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1; MSN OptimizedIE8;ENGB)

The work around in PHP I used was "OptimizedIE8". By tweaking my detection script, I added:

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'OptimizedIE8') )
{
$browser = 'ie8';
}

For those of you that like to avoid JavaScript and use PHP, I hope this helps. Others that use ASP, I'm sure you have something to combat this too.

If this still does not work on IE and my website then please let me know. Thanks.

3 comments:

nahid said...

But in my case I get the following signature for IE8:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

"MSN OptimizedIE8" is not there

Baldeep Birak said...

It's most likely that you are viewing a website using compatibility mode. That's why is says "compatible; MSIE 7.0"

I just tested it on my computer and see that switching between compatibility mode changes between "compatible; MSIE 8.0" and "compatible; MSIE 7.0".

In IE, can you go to tools menu -> compatibility view settings. Then make sure that the website you are on is not in the list. Also on my IE, I have none of the check boxes selected.

Baldeep said...

With IE9 onwards you have the developer tools (press F12) and you can change the "browser mode"