How to detect Internet Explorer with PHP

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 In PHP superglobal array $_SERVER there is usually HTTP_USER_AGENT element which contains browser-specific id string

Internet Explorer uses something like

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”.
Code
function Tr_detect_ie below returns true if Internet Explorer has been detected.

function Tr_detect_ie(){
    if (isset($_SERVER['HTTP_USER_AGENT']) && 
    (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
       return true;
    else
        return false;

you can use it like

<!-- function Tr_detect_ie (see code above) is pasted here  -->  if (Tr_detect_ie()) {  ?>
It seems, that your are using MSIE.Why not to switch to standard-complaint browser, like <a href="http://www.mozilla.com/firefox/">Firefox</a>?
<?php } 

For a speed optimization, 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.

About Techie Resource

I am a wordpress expert and Wordpress Consultant with years of Experience in wordpress Technology.

View all posts by Techie Resource →

Leave a Reply

Your email address will not be published. Required fields are marked *