What is PHP Device Detector?
51Degrees.mobi Device Detector for PHP provides intelligence about the device, operating system and browser accessing a web site at run time enabling developers and designers to adapt the initial response from the server. Existing web sites can be converted to support mobile devices quickly. The solution can be combined with emerging methodologies such as responsive design or progressive enhancement to produce great results quickly.
Auto Generated Source Code
Device Detector for PHP has been designed for easy deployment and to maximise performance in a pure PHP environment easily outperforming paid for cloud services which offer similar services. To achieve great performance the solution embeds code and data into combined files eliminating time consuming IO and memory operations. All the PHP files used are auto generated and optimised for performance. As a result the PHP files are not designed to be directly edited and may be difficult to follow. The documentation on this page provides the details about the implementation which developers might typically expect to find embedded in the source code. If you have any changes, questions or specific requirements please contact us.
Operational Overview
When an HTTP request is received and device data does not already exists in the session the solution will identify a family (or families) the device belongs to and assign a handler for each family. Handlers significantly improve performance over designs that do not perform this initial segmentation. For example an Android 3 based device will be assigned to an Android 3 handler, immediately narrowing the individual devices to be checked against.
Each handler will then check the requesting device against each of its device generating a score for each. The device with the lowest score will win and the properties associated with that device will be returned. A score of zero indicates an exact match.
Sometimes more than one device will be found with equal score. A different algorithm which is focused on matching the initial section of the useragent string is then used to narrow down a single device.
The matched device is stored in the session if available to avoid the need to repeat this process for subsequent requests.
Devices are checked in descending order of popularity based on real usage information to further improve performance for popular devices such as desktop browsers or iPhone (see Usage Data).
Using the PHP Device Detector
All the requesting device properties are stored in a variable called $_51d, which is populated automatically after including the 51Degrees.mobi PHP Device Detector script, as detailed in the ‘Quick Start’ section.
For more information on what keys are available, and their possible values please see the section on ‘Using the Meta Data’ and the Property Dictionary page.
The data within each key of the $_51d variable will either be a string value if the property can only hold a single value, or an array if a property may have multiple value. For instance, ‘HardwareName’ may return many values as a device may have a different name in different regions. Even if a particular device only has one value for such a property, it will still return an array. These properties are noted in the property dictionary with ‘L’, for list.
To utilize any particular key, use $_51d[‘PropertyName’], replacing PropertyName with the 51Degrees.mobi property name of your choice, as in the property dictionary linked above.
A single property can be found using this code:
if($_51d[‘IsMobile’] == ‘True’) { // This device is mobile }
As the values are always of type string, ‘True’ and ‘False’ values need to be evaluated as strings.
Note that if a property used in this way returns ‘Array’ it should be treated as a list property as below.
Specific values in list property can be found using the ‘isset’ function, but it can be viewed and sorted just like any other array can.
if(isset($_51d['CcppAccept']['audio/mp3']))
{
// The browser accepts mp3 files.
}
Note that ‘CcppAccept’ is a premium property, can only be used with premium data. Go to http://51degrees.mobi/Products/DeviceData.aspx for more information.
Hello World - Example Project
<?php
session_start();
include_once(‘path/to/51Degrees/51Degrees.mobi.php’);
include_once(‘path/to/51Degrees.mobi.devices.php’);
if($_51d[‘IsMobile’] == ‘True’)
{
//This device is a mobile. You can now use mobile stylesheets.
//Let’s print out the Height and Width of the screen.
echo ‘<br />ScreenHeight: ‘.$_51d[‘ScreenPixelsHeight’];
echo ‘<br />ScreenWidth: ‘.$_51d[‘ScreenPixelsWidth’];
}
?>
For more information about the meaning of data exposed via 51Degrees.mobi see the Property Dictionary.
Meta Data
The file 51Degrees.mobi.metadata.php provides meta data associated with the properties and values available in the dataset. Meta data can be used in user interface components to enable end users to easily understand the data they're working with. For example; Content Management Systems (CMS) can provide drop down lists of possible properties and values to create rules to determine which theme or skin should be used.
The global variable $_51d_meta_data, created by 51Degrees.mobi.metadata.php, provides an array of meta data. The property name is the array key.
The following table describes the $_51d_meta_data array and the information available.
| Field | Description |
| Description | Text description of the property and how it should be used. |
| URL | Hyperlink to more information about the property if available. |
| List | True to indicate that the property can have more than one value and an array of values will be returned. False to indicate a single value will be returned. |
| Values | Array of all possible values for the property. Key field is value name. i.e. True or False. |
The following table describes the array returned by the Values element of the property array.
| Field | Description |
| Description | Text description of the value and how it should be used. |
| URL | Hyperlink to more information about the value if available. |
The following example code shows how to access information related to the LayoutEngine property.
<!doctype html>
<html>
<head></head>
<body>
<p>
<?php
include('51Degrees-Premium/51Degrees.mobi.metadata.php');
?>
<table>
<tr><th colspan="2">Layout Engine<th></tr>
<tr><td>Description</td><td><?php echo $_51d_meta_data["LayoutEngine"]["Description"]; ?></td></tr>
<tr><td>List</td><td><?php echo $_51d_meta_data["LayoutEngine"]["List"]; ?></td></tr>
<tr><td>URL</td><td><?php echo $_51d_meta_data["LayoutEngine"]["URL"]; ?></td></tr>
<tr><th colspan="2">Possible Values</th></tr>
<?php
foreach ($_51d_meta_data["LayoutEngine"]["Values"] as $key => $value)
echo "<tr><td>".$key."</td><td>".$value["Description"]."</td></tr>";
?>
</table>
</body>
</html>
The meta data include should only be added to pages that require meta data and is entirely optional.
Usage Data
The following include will send usage information to 51Degrees.mobi.
include_once(‘path/to/51Degrees.mobi.usage.php’);
Usage information provides 51Degrees.mobi insight to improve the solutions performance, and identify new or less popular devices quickly. It is amalgamated with other data sources to bring you this solution. Highly efficient UDP messages are used to eliminate processing overhead in your environment.
Data Update
The detector has a built in method for downloading and installing new data for premium users.
Before proceeding, you will need to add your licence to your 51Degrees installation.
You can do this by saving your licence key to a simple text file and naming it 51D.lic. Note
that the name of the file isn't important, only that it is a txt file and has the .lic extension.
Now a licence is added all that's needed now is to activate an update. You will see the file
'51DUpdate.php'. Request this file from your browser to begin an update.
You will then see the browser showing update progress. If you see something like the following screen
the update was a success.

Troubleshooting
You may see an error indicating that memory ran out. To fix this you will need to increase memory_limit
in PHP.ini. By default the size is 128M (128mb). Change this to 256M and try again. If the same error
occurs try allowing more memory. See the
PHP Manual for more information.
Advanced Suggestions
These suggestions will improve the performance of your solution significantly and are highly recommended for production deployment.
APC/WinCache
To significantly increase performance, we recommend that you use APC or WinCache. The installation procedure changes depending upon your site host and server configuration. For full installation instructions, please see:
http://si2.php.net/manual/en/apc.installation.php
IIS/WebMatrix installations
For IIS and WebMatrix PHP users, it is recommended to use Microsoft’s WinCache. For full details, go to http://www.iis.net/download/WinCacheForPhp and use the link for the Web Platform Installer to install WinCache.
Windows XAMPP/Windows-Server run Apache installations
1) Download the APC installation at http://downloads.php.net/pierre/ (search for APC and download the version closest to your PHP installation. For example, for 5.3, use php_apc-3.1.10-5.3-vc9-x86.zip).
2) Extract the file in the /ts folder of the zip file to the ext folder of your PHP installation
Add the following line to your php.ini file under [Extensions]:
add extension=php_apc.dll
4) Open to the httpd.conf file in a text editor.
5) Replace all instances of
AllowOverride None
with
AllowOverride All
6) If Apache is running, stop it and start it back up again.
Unix/Linux Servers
Please ensure you are running PECL first. If this is installed and running, to install APC type the following into your command line:
sudo pecl install apc
You may need to restart Apache after the installation.
Sockets
UDP messages are used to transmit usage information when sockets are enabled. Most PHP environments by default will enable sockets. However, on some IIS or Windows Server PHP default installations (e.g. WebMatrix), the extension needs to be enabled manually.
Windows Servers
Open the PHP configuration file (php.ini) in the text editor of your choice and search for the following line:
;extension=php_sockets.dll
Simply uncomment this line like this:
extension=php_sockets.dll
Unix/Linux
Open the PHP configuration file (php.ini) in the text editor of your choice and search for the following line:
;extension=sockets.so
If this line is not available, then add, or otherwise uncomment this line like this:
extension=sockets.so