Browscap is an open project
dedicated to collecting an disseminating a "database" of browser capabilities --
actually a set of different files describing browser capablities. PHP has built-in
support for using these files via the get_browser()
function. This function requires that your php.ini
provides a
browscap
entry pointing to the PHP-specific
php_browscap.ini
file, which you can download from
the browscap site.
This class provides a features
adapter that calls get_browser()
in order to discover
mobile device capabilities to inject into UserAgent
device
instances.
First, if you haven't already, download the
php_browscap.ini file, and put it somewhere your web server can access it;
make sure the web server has permissions to read the file. Typically, you'll place this
in the same location as your php.ini
file.
Next, update your php.ini
file to add the following line:
browscap = /path/to/php_browscap.ini
Keep it simple
If you put your php_browscap.ini
file next to the
php.ini
file, you can omit the path information, and simply
specify the filename.
Next, simply provide configuration to your application as follows:
resources.useragent.mobile.features.classname = "Zend_Http_UserAgent_Device_Features_Browscap"
At this point, you're all set. You can access the browser information in a variety of ways. From within the MVC portion of your application, you can access it via the bootstrap. Within plugins, this is done by grabbing the bootstrap from the front controller.
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); $userAgent = $bootstrap->getResource('useragent');
From your action controller, use getInvokeArg()
to grab the
bootstrap, and from there, the user agent object.
$bootstrap = $this->getInvokeArg('bootstrap'); $userAgent = $bootstrap->getResource('useragent');
Within your view, you can grab it using the UserAgent
view
helper.
$userAgent = $this->userAgent();
Once you have the user agent object, you can query it for different capabilities. As one example, you may want to use an alternate layout script based on the user agent capabilities.
$device = $userAgent->getDevice(); $cssSupport = $device->getFeature('cssversion'); $jsSupport = $device->getFeature('javascript'); switch (true) { case ($jsSupport && $cssSupport >= 3): $layout->setLayout('layout-html5'); break; case ($jsSupport && $cssSupport < 3): $layout->setLayout('layout-xhtml'); break; case (!$jsSupport && $cssSupport < 3): $layout->setLayout('layout-html-transitional'); break; default: $layout->setLayout('layout-web-1'); break; }