Free, tested & ready to use examples : php detect check Internet explorer
AnyExample.com
 
Web anyexample.com
 

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, 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
<?php
function ae_detect_ie()
{
    if (isset(
$_SERVER['HTTP_USER_AGENT']) && 
    (
strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
        return 
true;
    else
        return 
false;
}
?>

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
<!-- function ae_detect_ie (see code above) is pasted here  -->
<?php  if (ae_detect_ie()) {  ?>
It seems, that your are using MSIE.
Why not to switch to standard-complaint brower, like 
<a href="http://www.mozilla.com/firefox/">Firefox</a>?
<?php }  ?>
tested by AnyExample.com on 2006-07-30
  • PHP 5.0.5 :: Microsoft Internet Explorer 6.0
  • PHP 5.0.5 :: Firefox 1.5
 


 
© AnyExample 2010
License | Privacy | Contact