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.