Get your user to update their browser

Follow @tristarweb

Today I am going to show you how to detect if the web user is using an out to date version of Internet Explorer. There are a few methods you can use to achieve this, in this tutorial I will use PHP.

At the top of your web page add the following:

<?php

if(preg_match('/(?i)msie [1-6]/',$_SERVER['HTTP_USER_AGENT'])) {
   echo 'This is IE 6';
  } elseif (preg_match('/(?i)msie [1-7]/',$_SERVER['HTTP_USER_AGENT'])){
   echo 'This is IE 7';
   }
   else {
   // Do Nothing
  }

?>

The above preg_match function searches the data returned from $_SERVER['HTTP_USER_AGENT'] for a matching pattern. The pattern is ‘msie [1-6]‘, msie [1-7]‘ and so on. You can test to see if this works by downloading an application called IE Tester. This will allow you to test your website in IE 6, 7 and 8.

You can customize the code by replacing the display message with a link to update the browser. For example:

<?php if(preg_match('/(?i)msie [1-8]/',$_SERVER['HTTP_USER_AGENT'])) : ?>
<p>You are using an out of date browser. Please update your browser <a href="http://windows.microsoft.com/en-GB/internet-explorer/products/ie/home">here</a>.</p>
<?php else : ?>
<?php endif; ?>

The above code will only display an update link if the user is using an Internet Explorer lower than version 8.

The advantage of using PHP to browser detect is that your not limited to just IE. You can check to see if the user is browsing in Firefox, Chrome etc. The example below will display a message if it has “Firefox” in its HTTP_USER_AGENT.

<?php
if (!stristr($_SERVER['HTTP_USER_AGENT'], "Firefox")) {
// Do Nothing
} else {
echo 'HTTP_USER_AGENT identified as Firefox';
}
?>

I hope you found these web development tips useful.

About robert

Hi my name is Rob Hills. At most times you will find me playing around with Magento and Wordpress. I am always looking to improve my web development knowledge – this ranges from PHP, Javascript, jQuery, XML etc. Other than web development – I produce Dance Music. My biggest achievement is getting my track played by Kutski on Radio 1..

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter