PHP Browser Detection

Tuesday, January 26th, 2010 7:14 pm

What is it?

PHP Browser Detection is a WordPress plugin used to detect a user’s browser. It could be used to send conditional CSS files for Internet Explorer, display different content or custom messages anywhere on the page, or to swap out flash for an image for iPhones.

Features

  • Get all browser information returned in an array.
  • Conditional statements to detect each version of Internet Explorer.
  • Conditional statements to detect if it is any mobile browser.
  • Conditional statements to detect if it is an iPhone.

Installation and Activation

Get The File:
Navigate to ‘Plugins-> Add New’. Search for ‘PHP Browser Detection’ and install using the plugin browser.
Or…

  • Download Plugin from WordPress
  • Use the plugin browser to upload the zip file.
  • You can also unzip ‘php-browser-detection.zip’ and manually upload the entire folder ‘php-browser-detection’ to your ‘/wp-content/plugins/’ folder.
  • Navigate to the ‘Plugins’ menu in the admin section.
  • Look for ‘PHP Browser Detection’ and activate it.

How To Use it In Your Theme Files

Within a theme file, you use the template tag:

$browserInfo=php_browser_info();

This will return an array with all the browser information. A full example:

  • [browser_name_pattern] => Mozilla/5.0 (Macintosh; *; *Mac OS X*; *; rv:1.9.2*) Gecko/* Firefox/3.6*
  • [browser_name_regex] => ^mozilla/5\.0 (macintosh; .*; .*mac os x.*; .*; rv:1\.9\.2.*) gecko/.* firefox/3\.6.*$
  • [browser] => Firefox
  • [version] => 3.6
  • [majorver] => 3
  • [minorver] => 6
  • [platform] => MacOSX
  • [alpha] =>
  • [beta] => 1
  • [win16] =>
  • [win32] =>
  • [win64] =>
  • [frames] => 1
  • [iframes] => 1
  • [tables] => 1
  • [cookies] => 1
  • [backgroundsounds] =>
  • [cdf] =>
  • [vbscript] =>
  • [javaapplets] => 1
  • [javascript] => 1
  • [activexcontrols] =>
  • [isbanned] =>
  • [ismobiledevice] =>
  • [issyndicationreader] =>
  • [crawler] =>
  • [cssversion] => 3
  • [supportscss] => 1
  • [aol] =>
  • [aolversion] => 0
  • [parent] => Firefox 3.6
// To access each part, just call the initial function...
$browserInfo=php_browser_info();

// ... then use the following as examples:
$browser = $browserInfo[browser];
$version = $browserInfo[version];
$platform = $browserInfo[platform];

… etc.

Conditional Statements

// To detect Internet Explorer ...
if (is_IE()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;
// To detect any version of Internet Explorer 6 ...
if (is_IE6()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;
// To detect any version of Internet Explorer 7 ...
if (is_IE7()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;
// To detect any version lower than Internet Explorer 6 ...
if (is_lt_IE6()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;
// To detect any version lower than Internet Explorer 7 ...
if (is_lt_IE7()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;
// To detect any Mobile Browser ...
if (is_mobile()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;
// To detect an iPhone ...
if (is_iphone()) : /* DO SOMETHING */; else :  /* DO SOMETHING ELSE */; endif;

An Example of how I use it

I place the following in header.php, just above the closing ‘</head>’ tag. I do this to make sure that the browser adjustments are the very last thing called and can correct anything added by random plugins.


< ?php

if (is_lt_IE8()) {
echo '<link rel="stylesheet" href="' . get_bloginfo ( 'template_url' ) . '/style/css/ie.css" type="text/css" media="screen" />';
};

if (is_safari()) {
echo '<link rel="stylesheet" href="' . get_bloginfo ( 'template_url' ) . '/style/css/safari.css" type="text/css" media="screen" />';
};

if (is_IE7()) {
echo '<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE7.js" type="text/javascript"></script>';
}
if (is_lt_IE7()) {
echo '<link rel="stylesheet" href="' . get_bloginfo ( 'template_url' ) . '/style/css/ie6.css" type="text/css" media="screen" />';
echo '<script type="text/javascript" src="' . get_bloginfo ( 'template_url' ) . '/js/unitpngfix.js"></script>';
}

?>

You can see that I create a folder called ‘style’ (which I also use to keep any images referenced by the css) and inside that, a folder called ‘css’. In that css folder, I add a file for each browser I need to target and use the above to only load that extra file when needed.

To simplify the header.php file, I have also started wrapping this all in a function called ‘php_browser_fixes()’, defined in my functions.php. Then I just need to include the one line calling the function.

Future of the Plugin

I’m hoping to build this into a one-stop fix for Internet Explorer. Basically I would add all the conditional statements I generally add, then put it all in one function called ‘fix_IE()’ or ‘IE_sucks()’ or ‘Bill_Gates_is_a_jerk()’… something like that.

The other option could be to just install that as a plugin and not even require the addition of the fix_IE function, just add the needed statements to header.php using wp_head(). If you don’t know what any of that means, don’t worry about it, but if you do, I would love to hear any feedback about what you are using for IE fixes and whether it would be better as an auto fix or to require the function to be called.

Feedback and Support

Any feedback is welcome but I can’t promise any real support. I developed this in my spare time and am simply offering it up as-is. I would definitely be interested in hearing about any bugs or ideas for improvement, though so feel free to leave any comments below.

Share/Bookmark

11 Comments to PHP Browser Detection

  • Joseph Crotty says:

    Well, I hooked it up and gave it a whirl. Dropped the following in a Template file:

    $browserInfo = php_browser_info();

    Here is the error running PHP 5.3.1:

    Warning: syntax error, unexpected $end, expecting ‘]’ in /Library/WebServer/Documents/ldev.naprotrack.com/wp-content/plugins/php-browser-detection/php_browscap.ini on line 49 in /Library/WebServer/Documents/ldev.naprotrack.com/wp-content/plugins/php-browser-detection/php-browser-detection.php on line 116…

    • Marty says:

      Hey Joseph,

      Thanks for the heads-up. After a quick Google search I found the answer. The issue is with a change in the way the ‘parse_ini_file()’ function called on line 116 of the plugin works in php 5.3.

      I guess 5.3 reads a semi-colon on that line 49 of php_browscap.ini and gets tripped up. To fix, replace this from line 116 of php-browser-detection.php:

      ————————————————
      $brows=parse_ini_file(realpath($browscap),true);
      ————————————————

      with this:

      ————————————————
      if (version_compare(PHP_VERSION, ’5.3.0′) >= 0) {
      $brows = parse_ini_file(realpath($browscap), true, INI_SCANNER_RAW);
      }else{
      $brows = parse_ini_file(realpath($browscap),true);
      }
      ——————-
      For the full explanation, you can read the forum post here: http://code.google.com/p/phpbrowscap/issues/detail?id=11.

      You got to me just in time before I upload to the WordPress repository for version 1.0 of the plugin.

      Thanks!

  • Bedo says:

    Hi Marty, really cool plugin, exactly what I was searching for, however I needed to detect the firefox subversions as well (because of the new css3 features)

    Here the code :

    ´function is_firefox_3 (){
    $browserInfo = php_browser_info();
    if(isset($browserInfo['browser']) && $browserInfo['browser']==’Firefox’ && $browserInfo['version'] == 3)
    return true;
    return false;
    }
    function is_firefox_3_5 (){
    $browserInfo = php_browser_info();
    if(isset($browserInfo['browser']) && $browserInfo['browser']==’Firefox’ && $browserInfo['version'] == 3.5)
    return true;
    return false;
    }
    function is_firefox_3_6 (){
    $browserInfo = php_browser_info();
    if(isset($browserInfo['browser']) && $browserInfo['browser']==’Firefox’ && $browserInfo['version'] == 3.6)
    return true;
    return false;

    You could may consider this in the next release, maybe it will help somebody else as well.

    Cheers

    • Marty says:

      Thanks. Glad it is helping. Tests for sub versions was definitely next. But you saved me some time it looks like. I will be adding a test for JavaScript as well.

  • Toby says:

    Sorry for being a dunce here but can someone post an example of a conditional statement that loads an alternate CSS file in WordPress? Would that go in header.php? I keep crashing my blog when trying to use this.

    http://www.festival.si.edu/blog/

    Thanks!
    Toby

  • Tsem says:

    Hi there, this looks like exactly what we need!

    We’d like to use the ability mentioned “Display different content or custom messages anywhere on the page, or to swap out flash for an image for iPhones.”

    Could you give me a summary of how to do this in a wordpress page to display alternate content for iphone users?

    Thanks!

    • marty says:

      Unfortunately php code does not show up very well in the comments.
      But you would just use the if statements provided to test for an iphone

      if ( is_iphone() ) :
      -Show a Quicktime movie-
      else :
      -Show a flash movie-
      endif;

      You’ll have to use the included examples or look up php if statements for help with the correct formatting.

      • Tsem says:

        Thanks for the response, unfortunately I think I am running into the issue where wordpress does not execute php statements inserted into the body.

        would anyone have a real example of how this is done in context of what should be entered in the page edit mode?

Trackbacks/Pingbacks