Change Joomla module based on browser detection

(11 votes, average 3.45 out of 5)

Don’t tear your hair out if you can’t make one of your modules display correctly on Internet Explorer. Just design an alternative (simplified) version of the module, and use the following rules to display different modules for IE browsers. You can just as easily detect other browsers such as the iPhone and present different information just for them – or hide modules to suit the smaller browser.

With JomGenius

This technique is available from MetaMod 2.4 onwards.

Each rule is just 1 or 2 lines and can be used independently — just customise the XXX-YYY at the end of each rule to return the module id that you want to use in each case.

Note: see the JomGenius "core" page for more details on using $core_genius, and the parameters available.

All values for "browser_os": windows, mac, linux, bot, console [e.g. gaming consoles]

All values for "browser_type": firefox, ie, chrome, opera, omniweb, ipod, iphone, ipad, android, googlebot, google mediapartners, yahoo slurp, msnbot, teoma, playstation, wii, safari

All values for "browser": With version numbers (X is the major version number): firefoxX, ieX, chromeX, operaX, omniwebX, safariX
All others: ipod, iphone, ipad, android, googlebot, google mediapartners, yahoo slurp, msnbot, teoma, playstation, wii

Values for "browser_version": JomGenius can detect version numbers for the following browsers only: IE, Firefox, Opera, Chrome, OmniWeb, Safari (not including iPod, iPhone, iPad). You may wish to compare these numbers using <, <=, >, >= etc.

// use module for any version of Internet Explorer
if ( $core_genius->check( "browser_type = ie" ) ) return XXX;
// use module for any version of Internet Explorer, and an
// alternative for any other browser/bot:
if ( $core_genius->check( "browser_type = ie" ) ) return XXX;
else return YYY;
// use module for any version of Firefox, and an
// alternative for any other browser/bot:
if ( $core_genius->check( "browser_type = firefox" ) ) return XXX;
else return YYY;
// detect Internet Explorer v6 explicitly
if ( $core_genius->check( "browser = ie6" ) ) return XXX;
// detect any sub-version of Explorer 5 (e.g. 5.0, 5.5)
if ( $core_genius->check( "browser_type = ie" )
   and $core_genius->check( "browser_version >= 5" )
   and $core_genius->check( "browser_version < 6" ) ) return XXX;
// detect any browser on the Mac platform
if ( $core_genius->check( "browser_os = mac" ) ) return XXX;
// detect any browser on Windows
if ( $core_genius->check( "browser_os = windows" ) ) return XXX;
// detect any browser on Linux
if ( $core_genius->check( "browser_os = linux" ) ) return XXX;
// detect any bot (e.g. googlebot or other search bots)
if ( $core_genius->check( "browser_os = bot" ) ) return XXX;
// detect iphones, ipods and ipads. You can stack several browsers together like this:
if ( $core_genius->check( "browser_type = iphone, ipod, ipad" ) ) return XXX;
// detect google mediapartners bot (as used for determining appropriate adsense advertising)
if ( $core_genius->check( "browser_type = google mediapartners" ) ) return XXX;
// detect google search bot
if ( $core_genius->check( "browser_type = googlebot" ) ) return XXX;
// detect all major bots
if ( $core_genius->check( "browser_os = bot" ) ) return XXX;

Pre-JomGenius

If you have MetaMod 2.4 or more recent, try the "JomGenius" version instead of this one.

Simple check for Internet Explorer; use an alternative module if found:

$UA  = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  $IE  = strstr($UA, 'MSIE') ? true : false;
  $IEV = $IE ? preg_split('/msie/i', $UA) : false;
  $IEV = $IEV ? floatval($IEV[1]) : false;
  // Customise the module return numbers as appropriate for your site.
  // You can use any one or more of the 3 following rules as necessary.
  if ($IE and $IEV <= 5) return 55; // any version of IE 5 or less
  if ($IE and $IEV == 6) return 55; // IE6 only
  if ($IE) return 55; // any version (at all) of IE
  return 66; // if none of the preceding rules apply, use this module id.

A full check of the major browser types (Chrome, iPhone, iPad, Safari, Opera, Firefox, Internet Explorer), checking for versions. Note that if you want to check for Safari, then you also need the lines to check for Chrome ($CH) first, because the Chrome user agent string contains the word “Safari”.

$UA  = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$CH  = strstr($UA, 'Chrome/') ? true : false;
$CHV = $CH ? preg_match('#Chrome/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)#',
         $UA, $matches) : false;
$CHV = $CHV ? $matches[1] : false;
$IPD = strstr($UA, 'iPad;') ? true : false;
$IP  = strstr($UA, 'iPhone') ? true : false;
$SF  = !$CH &amp;&amp; strstr($UA, 'Safari') ? true : false;
$OP  = strstr($UA, 'Opera') ? true : false;
$OPV = $OP ? preg_split('/opera\//i', $UA) : false;
$OPV = $OPV ? floatval($OPV[1]) : false;
$FF  = strstr($UA, 'Firefox') ? true : false;
$FFV = $FF ? preg_split('/firefox\//i', $UA) : false;
$FFV = $FFV ? floatval($FFV[1]) : false;
$IE  = strstr($UA, 'MSIE') ? true : false;
$IEV = $IE ? preg_split('/msie/i', $UA) : false;
$IEV = $IEV ? floatval($IEV[1]) : false;
 
// customise the module return numbers as appropriate for your site
if ($IE and $IEV >= 7) return 55; // any version of IE greater or equal to 7
if ($IE and $IEV >= 6) return 55; // any version of IE greater or equal to 6
if ($IE) return 55; // any version (at all) of IE
if ($OP) return 66; // any version of Opera
if ($FF) return 77; // any version of Firefox
if ($IP) return 88; // any version of iPhone
if ($IPD) return 99; // any version of iPad
if ($SF) return 101; // any version of Safari (excl Chrome but incl iPhone and iPad)
if ($CH) return 102; // any version of Google Chrome
if ($CHV == "0.2.149.27") return 103; /* specific Chrome version */ 

Just check for Safari, with comprehensive version support:

$UA  = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$CH  = strstr($UA, 'Chrome/') ? true : false; /* rule out "Chrome" */
$SF  = !$CH &amp;&amp; strstr($UA, 'Safari') ? true : false;
// if you want to check for specific versions, here's some sample code:
if ($SF) {
  $early_versions = array(
    "85"=>1.0,
    "125"=>1.2,
    "312"=>1.3,
    "412"=>2.0,
    "416"=>2.0,
    "417"=>2.0,
    "419"=>2.0
  );
  $SFV = preg_match( '#(Version/([0-9.]+))? Safari/([0-9]+)#', $UA, $matches);
  if ( count($matches) == 4 ) {
    if ( array_key_exists( $matches[3], $early_versions ) ) {
      $SFV = $early_versions[ $matches[3] ];
    } else if ($matches[2] != '') {
      $SFV = $matches[2];
    }
  }
}