One common problem in web development is to ensure that your site is usable on both desktop computers and mobile devices, such as mobile phones. A related issue is that, even if you are aiming only at phones, you will probably want to make your site viewable across a wide range of different models running different operating systems, such as Android, iPhone OS and Symbian. There are a number of reasons why this might require extra effort.
Each browser sends a so-called user agent string which is a unique description identifying the browser, version and operating system. For example for this browser it is
CCBot/1.0 (+http://www.commoncrawl.org/bot.html)We can obtain the user agent string quite easily in PHP:
<?php $a = $_SERVER['HTTP_USER_AGENT']; echo "The user agent is " .$a; ?>Then, we can generate different code for the website depending on the user agent string.
There is a remaining problem with this approach, however: there are many different mobile devices and browsers and clearly it would be a big job to try and test for all. This is where the WURFL comes in.
The WURFL (see here) is "an XML configuration file which contains information about capabilities and features of several wireless devices" (wurfl.sourceforge.net). In other words, the WURFL is an XML file which maps individual mobile devices to their capabilities (e.g. do they support XHTML?). You can write a web application which queries this XML file to look up the capabilities of a given mobile device.
WURFL can be used from a range of languages such as PHP and Java (see the WURFL site). From PHP the following procedure is performed:
See here for a full example. Here is an explanation of each line of code:
define('WURFL_DIR','/var/www/wurfl-php-1.2.1');
In this line, we define our WURFL installation directory.
require_once(WURFL_DIR."/WURFL/Application.php");The first line of a WURFL-using PHP script needs to link in the PHP WURFL library. The path to the PHP WURFL library is as shown above.
$config = WURFL_DIR."/examples/resources/wurfl-config.xml";This line reads in a configuration file for the WURFL. This controls things such as how the WURFL is cached for fast access (e.g. whether to use a file cache or a database cache).
$factory=new WURFL_WURFLManagerFactory (new WURFL_Configuration_XmlConfig($config)); $mgr=$factory->create(true);These lines create the WURFL manager - a PHP variable that provides a handle on the whole WURFL system. This code will be common to all WURFL applications.
$device = $mgr->getDeviceForUserAgent($_SERVER["HTTP_USER_AGENT"]);
echo "Device brand name " . $device->getCapability("brand_name");
echo "<br />";
echo "Device model name " . $device->getCapability("model_name");
echo "<br />";
if ($device->getCapability("jpg") == "true")
{
echo "This device supports JPEG images.";
}
else
{
echo "This device does not support JPEG images.";
}
?>
Device brand name Nokia Device model name N95 This device supports JPEG images.
The most important concept in the WURFL is the capability. A capability is a particular feature that the mobile device may or may not support, and we can query any capability. The way in which we reference a capability is shown above.
Full documentation of all capabilities is available here. There are a number of capability categories, which are described in detail on the WURFL site; they include such things as:
Some specific examples: