Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: If internet explorer 6, 7, 8 , or 9

Tags:

browser

php

I want to do a conditional in PHP for the different versions of Internet Explorer along the lines of:

if($browser == ie6){ //do this} elseif($browser == ie7) { //dothis } elseif...

I have seen many variations on similar code, but looking for something super simple that is very easy to code to do some simple if and else and do different things.

Thanks

EDIT: I need this to show some different messages to users so CSS conditionals etc are no good.

like image 253
Cameron Avatar asked Mar 14 '11 17:03

Cameron


People also ask

Is IE 9 supported?

For versions of Windows where Internet Explorer 9 was the final version of Internet Explorer available, support ended alongside the end of support for that version of Windows. On January 14, 2020, Microsoft released the final IE9 update for Windows Server 2008, marking the end of IE9 support on all platforms.

Is IE 6 still supported?

Internet Explorer 6 was the last version to be called Microsoft Internet Explorer. The software was rebranded as Windows Internet Explorer starting in 2006 with the release of Internet Explorer 7. Internet Explorer 6 is no longer supported, and is not available for download from Microsoft.

Is IE8 supported?

On April 9, 2019, Microsoft released the final IE8 update for Windows Embedded POSReady 2009, the last supported version of Windows based on Windows XP, marking the end of IE8 support on all platforms.

Does IE8 support HTML5?

IE8 beta 2 doesn't implement the HTML5 parsing algorithm or the new elements (no <canvas> or <video> support). There are also bug fixes that align IE8 better with HTML5. so the answer is that for all fits and purposes, IE8 does not support html5 - just some randome bits and pieces of it.


2 Answers

This is what I ended up using a variation of, which checks for IE8 and below:

if (preg_match('/MSIE\s(?P<v>\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {     // Browsers IE 8 and below } else {     // All other browsers } 
like image 94
Cameron Avatar answered Sep 20 '22 22:09

Cameron


A version that will continue to work with both IE10 and IE11:

preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches); if(count($matches)<2){   preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches); }  if (count($matches)>1){   //Then we're using IE   $version = $matches[1];    switch(true){     case ($version<=8):       //IE 8 or under!       break;      case ($version==9 || $version==10):       //IE9 & IE10!       break;      case ($version==11):       //Version 11!       break;      default:       //You get the idea   } } 
like image 30
Doug Avatar answered Sep 23 '22 22:09

Doug