PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Quick Start

First, you will need to download the php_browscap.ini if you do not already have it installed on your system:

We suggest that you put the file in the same directory as your php.ini.

Next, notify PHP of the browscap file via the browscap setting. Because this setting is of type PHP_INI_SYSTEM, this must be done either in your php.ini or web server configuration; it cannot be done in htaccess or via ini_set(). We recommend adding it to your php.ini.

browscap = /path/to/php_browscap.ini

If you put php_browscap.ini in the same directory as your php.ini, this can be reduced to simply:

browscap = php_browscap.ini

Now, edit your application.ini to add the following lines to your [production] section:

resources.useragent.mobile.features.classname = "Zend_Http_UserAgent_Device_Features_Browscap"

At this point, everything is setup. Each request will contain detailed information on the user agent.

You can access this 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.

<?php
$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.

<?php
$bootstrap 
$this->getInvokeArg('bootstrap');
$userAgent $bootstrap->getResource('useragent');

Within your view, you can grab it using the UserAgent view helper.

<?php
$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.

<?php
$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;
}

Finally, each device will often have a large number of capabilities not immediately represented in the device interface. You can query these using the hasFeature() and getFeature() methods, or simply grab all of them with getAllFeatures().

<?php
if ($userAgent->hasFeature('mp3') && $userAgent->getFeature('mp3')) {
    
// embed HTML5 audio tag...
}

Zend Framework