-
Notifications
You must be signed in to change notification settings - Fork 80
Adding & Using Detector With Your Application
Please note: Detector has not been thoroughly tested yet so caution should be used when including it in any production or near production system.
To add Detector simply follow these steps:
- Either download or checkout the Detector repository.
- Copy the
Detector/
directory found in thelib/
directory into your project. - Make sure the Detector directories
user-agents/core/
,user-agents/extended/
, andconfig
are writable by your web server. This is where profiles & configuration information are stored. - Update ua-parser-php so it has the latest
regexes.yaml
file. Follow the directions in the README. - 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. - If you will be using
per-request
tests make sure to copyfeatures.js.php
(found inutil/js-include/
) to a public directory and reference it in the<head>
of your HTML
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).