Asked  7 Months ago    Answers:  5   Viewed   55 times

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.

 Answers

24

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

if (preg_match('/MSIEs(?P<v>d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {
    // Browsers IE 8 and below
} else {
    // All other browsers
}
Wednesday, March 31, 2021
 
leebriggs
answered 7 Months ago
91
<?php 
 echo $_SERVER['HTTP_USER_AGENT'];
 ?> 

As Palantir says, additionally have a look at the get_browser function, where you can check also capabilities enabled in the browser.

Wednesday, March 31, 2021
 
michele
answered 7 Months ago
93

To identify the user's browser server-side, you'll have to parse the $_SERVER['HTTP_USER_AGENT'] variable...


... Or, probably better, use the get_browser function -- just note you'll have to configure something in php.ini, or you'll get this kind of warning :

Warning: get_browser() [function.get-browser]: browscap ini directive not set

Like the PHP manual page says :

Note : In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system. browscap.ini is not bundled with PHP, but you may find an up-to-date ยป php_browscap.ini file here.

While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.


After downloading that file and adding this line to php.ini :

browscap = /home/squale/developpement/tests/temp/php_browscap.ini

The following portion of code :

var_dump(get_browser(null, true));

Gives me :

array
  'browser_name_regex' => string '^mozilla/5.0 (x11; .*linux.*; .*rv:1.9.*) gecko/.*$' (length=53)
  'browser_name_pattern' => string 'Mozilla/5.0 (X11; *Linux*; *rv:1.9*) Gecko/*' (length=44)
  'parent' => string 'Mozilla 1.9' (length=11)
  'platform' => string 'Linux' (length=5)
  'browser' => string 'Mozilla' (length=7)
  'version' => string '1.9' (length=3)
  'majorver' => string '1' (length=1)
  'minorver' => string '9' (length=1)
  'alpha' => string '1' (length=1)
  'frames' => string '1' (length=1)
  'iframes' => string '1' (length=1)
  'tables' => string '1' (length=1)
  'cookies' => string '1' (length=1)
  'javaapplets' => string '1' (length=1)
  'javascript' => string '1' (length=1)
  'cssversion' => string '2' (length=1)
  'supportscss' => string '1' (length=1)
  'beta' => string '' (length=0)
  'win16' => string '' (length=0)
  'win32' => string '' (length=0)
  'win64' => string '' (length=0)
  'backgroundsounds' => string '' (length=0)
  'cdf' => string '' (length=0)
  'vbscript' => string '' (length=0)
  'activexcontrols' => string '' (length=0)
  'isbanned' => string '' (length=0)
  'ismobiledevice' => string '' (length=0)
  'issyndicationreader' => string '' (length=0)
  'crawler' => string '' (length=0)
  'aol' => string '' (length=0)
  'aolversion' => string '0' (length=1)

on firefox 3.5 ; and :

array
  'browser_name_regex' => string '^.*$' (length=4)
  'browser_name_pattern' => string '*' (length=1)
  'browser' => string 'Default Browser' (length=15)
  'version' => string '0' (length=1)
  'majorver' => string '0' (length=1)
  'minorver' => string '0' (length=1)
  'platform' => string 'unknown' (length=7)
  'alpha' => string '' (length=0)
  ....
  'aol' => string '' (length=0)
  'aolversion' => string '0' (length=1)

On a recent version (4.0.203.2) of google chrome for Linux -- well, considering it's some kind of nightly build, I suppose it's normal that it's not recognized...

As a reference, here is it's user-agent string :

string 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.203.2 Safari/532.0' (length=109)


This show that get_browser is nice ; but maybe not perfect for some cutting-edge test browser -- still, should work fine with most "common" browsers, I suppose...

Saturday, May 29, 2021
 
Norgul
answered 5 Months ago
33

You could try IETester.

Update In 2015 this is now the wrong answer to this question. See the accepted answer of the duplicate question linked above for a better solution (https://stackoverflow.com/a/574465/70795)

Sunday, August 8, 2021
 
BrunoRamalho
answered 3 Months ago
92

Install Virtual PC (now a free download) and one of the disk images from this page that provides you with a vanilla install of XP with the browser you want to test.

Friday, October 8, 2021
 
truemp
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :