PHP Browser Detection

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:
[sourcecode lang='php']
$browserInfo=php_browser_info();
[/sourcecode]
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

[sourcecode lang='php']
// 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];

[/sourcecode]

… etc.

Conditional Statements

[sourcecode lang='php']
// 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;
[/sourcecode]

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.

[sourcecode lang='php']

< ?php

if (is_lt_IE8()) {
echo '’;
};

if (is_safari()) {
echo ”;
};

if (is_IE7()) {
echo ”;
}
if (is_lt_IE7()) {
echo ”;
echo ”;
}

?>

[/sourcecode]

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.

Powered by PhotographyBlogSites