PHPackages                             fr05t1k/device-detector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. fr05t1k/device-detector

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

fr05t1k/device-detector
=======================

The Universal Device Detection library, that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, media players, mobile apps, feed readers, libraries, etc), operating systems, devices, brands and models.

3.12.2.beta1(6y ago)11.8kLGPL-3.0-or-laterPHPPHP &gt;=5.5

Since Apr 3Pushed 6y agoCompare

[ Source](https://github.com/fr05t1k/device-detector)[ Packagist](https://packagist.org/packages/fr05t1k/device-detector)[ Docs](https://matomo.org)[ RSS](/packages/fr05t1k-device-detector/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (6)Versions (66)Used By (0)

DeviceDetector
==============

[](#devicedetector)

[![Latest Stable Version](https://camo.githubusercontent.com/b220d646343aa1de1aa35681bd262948cb5a061e30da670b8c776b05c59fd299/68747470733a2f2f706f7365722e707567782e6f72672f706977696b2f6465766963652d6465746563746f722f762f737461626c65)](https://packagist.org/packages/piwik/device-detector)[![Latest Unstable Version](https://camo.githubusercontent.com/aab9c35d5580f4567b75d42e4939b3a0815ee8ca5f01ca0896673cab43ee6fa5/68747470733a2f2f706f7365722e707567782e6f72672f706977696b2f6465766963652d6465746563746f722f762f756e737461626c65)](https://packagist.org/packages/piwik/device-detector)[![Total Downloads](https://camo.githubusercontent.com/a6a167e16ca1f6880c98f61369d300ef80176e53c1c193fff017cff0566006c4/68747470733a2f2f706f7365722e707567782e6f72672f706977696b2f6465766963652d6465746563746f722f646f776e6c6f616473)](https://packagist.org/packages/piwik/device-detector)[![License](https://camo.githubusercontent.com/54cdaf4ad1d60a204b143ddcd5926d99b5236668cd85b8f048435bf409b1baa3/68747470733a2f2f706f7365722e707567782e6f72672f706977696b2f6465766963652d6465746563746f722f6c6963656e7365)](https://packagist.org/packages/piwik/device-detector)

Code Status
-----------

[](#code-status)

[![Build Status](https://camo.githubusercontent.com/2002d129aab80f931796bd1ce04e100b8f3e0bf6be9c0a43d2a13968634ee0cb/68747470733a2f2f7472617669732d63692e6f72672f6d61746f6d6f2d6f72672f6465766963652d6465746563746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/matomo-org/device-detector)[![Code Coverage](https://camo.githubusercontent.com/93e5f18ff74f6cd4f04de55fdef7b07daa1b241da8b686ea861ef18fb3fd38cd/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f706977696b2f6465766963652d6465746563746f722f62616467652e706e67)](https://coveralls.io/r/piwik/device-detector)[![Average time to resolve an issue](https://camo.githubusercontent.com/7f908d238e535f477f7df93ee4b630b400d60b37b0cd65d9ef77aa53760a26ce/687474703a2f2f697369746d61696e7461696e65642e636f6d2f62616467652f7265736f6c7574696f6e2f6d61746f6d6f2d6f72672f6465766963652d6465746563746f722e737667)](http://isitmaintained.com/project/matomo-org/device-detector "Average time to resolve an issue")[![Percentage of issues still open](https://camo.githubusercontent.com/3a89a04c4b5ac0085e9c6ab84a28b5bac27c746a0a34254ef058e7dec2cc4f10/687474703a2f2f697369746d61696e7461696e65642e636f6d2f62616467652f6f70656e2f6d61746f6d6f2d6f72672f6465766963652d6465746563746f722e737667)](http://isitmaintained.com/project/matomo-org/device-detector "Percentage of issues still open")

Description
-----------

[](#description)

The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, feed readers, media players, PIMs, ...), operating systems, brands and models.

Usage
-----

[](#usage)

Using DeviceDetector with composer is quite easy. Just add piwik/device-detector to your projects requirements. And use some code like this one:

```
require_once 'vendor/autoload.php';

use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;

// OPTIONAL: Set version truncation to none, so full versions will be returned
// By default only minor versions will be returned (e.g. X.Y)
// for other options see VERSION_TRUNCATION_* constants in DeviceParserAbstract class
DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);

$userAgent = $_SERVER['HTTP_USER_AGENT']; // change this to the useragent you want to parse

$dd = new DeviceDetector($userAgent);

// OPTIONAL: Set caching method
// By default static cache is used, which works best within one php process (memory array caching)
// To cache across requests use caching in files or memcache
// $dd->setCache(new Doctrine\Common\Cache\PhpFileCache('./tmp/'));

// OPTIONAL: Set custom yaml parser
// By default Spyc will be used for parsing yaml files. You can also use another yaml parser.
// You may need to implement the Yaml Parser facade if you want to use another parser than Spyc or [Symfony](https://github.com/symfony/yaml)
// $dd->setYamlParser(new DeviceDetector\Yaml\Symfony());

// OPTIONAL: If called, getBot() will only return true if a bot was detected  (speeds up detection a bit)
// $dd->discardBotInformation();

// OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
// $dd->skipBotDetection();

$dd->parse();

if ($dd->isBot()) {
  // handle bots,spiders,crawlers,...
  $botInfo = $dd->getBot();
} else {
  $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
  $osInfo = $dd->getOs();
  $device = $dd->getDeviceName();
  $brand = $dd->getBrandName();
  $model = $dd->getModel();
}
```

Instead of using the full power of DeviceDetector it might in some cases be better to use only specific parsers. If you aim to check if a given useragent is a bot and don't require any of the other information, you can directly use the bot parser.

```
require_once 'vendor/autoload.php';

use DeviceDetector\Parser\Bot AS BotParser;

$botParser = new BotParser();
$botParser->setUserAgent($userAgent);

// OPTIONAL: discard bot information. parse() will then return true instead of information
$botParser->discardDetails();

$result = $botParser->parse();

if (!is_null($result)) {
    // do not do anything if a bot is detected
    return;
}

// handle non-bot requests
```

Using without composer
----------------------

[](#using-without-composer)

Alternatively to using composer you can also use the included `autoload.php`. This script will register an autoloader to dynamically load all classes in `DeviceDetector` namespace.

Device Detector requires a YAML parser. By default `Spyc` parser is used. As this library is not included you need to include it manually or use another YAML parser.

```
