Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Adding & Using Detector With Your Application

dmolsen edited this page May 29, 2012 · 7 revisions

Please note: Detector has not been thoroughly tested yet so caution should be used when including it in any production or near production system.

Adding Detector to Your Application

To add Detector simply follow these steps:

  1. Either download or checkout the Detector repository.
  2. Copy the Detector/ directory found in the lib/ directory into your project.
  3. Make sure the Detector directories user-agents/core/, user-agents/extended/, and config are writable by your web server. This is where profiles & configuration information are stored.
  4. Update ua-parser-php so it has the latest regexes.yaml file. Follow the directions in the README.
  5. Include <?php require('path/to/Detector/Detector.php');> at the very start of your script. Detector will then automatically run on each request and create the $ua variable that holds all of the attributes for a particular browser profile.
  6. If you will be using per-request tests make sure to copy features.js.php (found in util/js-include/) to a public directory and reference it in the <head> of your HTML

Using Detector With Your Application

In order to access the features you need to use the $ua object that is automatically created with the inclusion of Detector in your application. Simply use the $ua object in the same way that you would have used the Modernizr object on the client-side:

if ($ua->svg) {
    ...
} elseif ($ua->canvas) {
    ...
}

See the full list of browser features that are tested by Modernizr and available with Detector. All of the features tested with ua-parser-php are also available from the $ua variable (e.g. $ua->isMobile or $ua->browserFull)

Some features, (in particular video, audio, input, and inputtypes) have sub-features, so these are available as nested PHP objects:

if ($ua->inputtypes->search) {
    print "<input type='search' ...";
} else {
    print "<input type='text' ...";
}

All features are returned as integer 1 or 0 for true or false, so they can be used in logical evaluations in PHP. Sub-features can return 1, 0, or a real value (e.g. screen width).