How to detect Internet Explorer with PHP

abstract
It is possible to use PHP to check whether your site visitor uses Internet Explorer and output some IE-specific text or HTML/CSS markup.
compatible
PHP 4.1.0 or newer

In PHP superglobal array $_SERVER there is usually HTTP_USER_AGENT element which contains browser-specific id string

Internet Explorer uses something like

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”

as id string

Internet Explorer uses something like

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”

as id-string, depending on platform and version. Every version of IE has “MSIE” part in HTTP_USER_AGENT string, so it is possible to detect IE simply by scanning $_SERVER['HTTP_USER_AGENT'] for string “MSIE”.

Example: function ae_detect_ie below returns true if Internet Explorer has been detected.
source code: php

For a maximum speed, we use strpos function instead of strstr, preg_match or other PHP string-search functions. Note, that if you want to check for IE in several places, it is better to store function value in some variable, to avoid serveral similar string-searches.

Here is the example of detecting Internet Explorer and suggesting user to get another browser:
source code: php

It seems, that your are using MSIE.
Why not to switch to standard-complaint brower, like
Firefox?

tested
PHP 5.0.5 :: Microsoft Internet Explorer 6.0
PHP 5.0.5 :: Firefox 1.5

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.