Wednesday, March 14, 2012

Detect Browser Info

When it comes to detecting browser info in PHP, we stick to the basic by using get_browser() but it doesn't properly display the browser types/names. If you're using Joomla, you'll most likely use the Joomla browser detection library.

Native PHP:
$browser = get_browser(null, true);
print_r($browser);
Array
(
  [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
  [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
  [parent] => Firefox 0.9
  [platform] => WinXP
  [browser] => Firefox
  [version] => 0.9
  [majorver] => 0
  [minorver] => 9
  [cssversion] => 2
  [frames] => 1
  [iframes] => 1
  [tables] => 1
  [cookies] => 1
  [backgroundsounds] =>
  [vbscript] =>
  [javascript] => 1
  [javaapplets] => 1
  [activexcontrols] =>
  [cdf] =>
  [aol] =>
  [beta] => 1
  [win16] =>
  [crawler] =>
  [stripper] =>
  [wap] =>
  [netclr] =>
)

Joomla:
jimport('joomla.environment.browser');
$browser =& JBrowser::getInstance();

echo $browser->getBrowser() . " " . $browser->getMajor();
konqueror 413

Yes, the above codes can detect browser info but they can't properly detect if the browser is a Chrome or Safari. So I look for another solution for and came across in this blog. You can download there the browser.php file where it detects the browser name and version almost perfectly.

require_once('Browser.php');
$browser = new Browser();

echo $browser->getBrowser() . " " . $browser->getVersion();
Firefox 10.0.2

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...