PHPackages                             piwik/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. piwik/device-detector

Abandoned → [matomo/device-detector](/?search=matomo%2Fdevice-detector)Library[Parsing &amp; Serialization](/categories/parsing)

piwik/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.

6.5.0(3mo ago)3.5k12.0M—9.2%507[63 issues](https://github.com/matomo-org/device-detector/issues)[12 PRs](https://github.com/matomo-org/device-detector/pulls)20LGPL-3.0-or-laterPHPPHP ^7.2|^8.0CI passing

Since Apr 3Pushed 1mo ago91 watchersCompare

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

READMEChangelog (10)Dependencies (9)Versions (117)Used By (20)

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

[](#devicedetector)

[![Latest Stable Version](https://camo.githubusercontent.com/0a2905c82ec09a1805d64622511db680765fe573d653ed124fd3397877c4eb65/68747470733a2f2f706f7365722e707567782e6f72672f6d61746f6d6f2f6465766963652d6465746563746f722f762f737461626c65)](https://packagist.org/packages/matomo/device-detector)[![Total Downloads](https://camo.githubusercontent.com/206d70ce7438e04973fdc63c860e2533422ef7a69a3519409e4fa86415cd0304/68747470733a2f2f706f7365722e707567782e6f72672f6d61746f6d6f2f6465766963652d6465746563746f722f646f776e6c6f616473)](https://packagist.org/packages/matomo/device-detector)[![License](https://camo.githubusercontent.com/55d1b3942589877e52db26286b9a16526aafe7e73cb06e3a3d68b8d3575f6dc0/68747470733a2f2f706f7365722e707567782e6f72672f6d61746f6d6f2f6465766963652d6465746563746f722f6c6963656e7365)](https://packagist.org/packages/matomo/device-detector)

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

[](#code-status)

[![PHPUnit](https://github.com/matomo-org/device-detector/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/matomo-org/device-detector/actions/workflows/phpunit.yml?branch=master "PHPUnit")[![PHPStan](https://github.com/matomo-org/device-detector/actions/workflows/phpstan.yml/badge.svg?branch=master)](https://github.com/matomo-org/device-detector/actions/workflows/phpstan.yml?branch=master "PHPStan")[![PHPCS](https://github.com/matomo-org/device-detector/actions/workflows/phpcs.yml/badge.svg?branch=master)](https://github.com/matomo-org/device-detector/actions/workflows/phpcs.yml?branch=master "PHPCS")[![YAML Lint](https://github.com/matomo-org/device-detector/actions/workflows/yamllint.yml/badge.svg?branch=master)](https://github.com/matomo-org/device-detector/actions/workflows/yamllint.yml?branch=master "YAML Lint")[![Validate regular Expressions](https://github.com/matomo-org/device-detector/actions/workflows/regular_expressions.yml/badge.svg?branch=master)](https://github.com/matomo-org/device-detector/actions/workflows/regular_expressions.yml?branch=master "Validate regular Expressions")

[![Average time to resolve an issue](https://camo.githubusercontent.com/b70a4a348c1fec2f3c63040254be47d3af24a357582681799f63cdd4cb043c64/68747470733a2f2f7777772e697369746d61696e7461696e65642e636f6d2f62616467652f7265736f6c7574696f6e2f6d61746f6d6f2d6f72672f6465766963652d6465746563746f722e737667)](https://www.isitmaintained.com/project/matomo-org/device-detector "Average time to resolve an issue")[![Percentage of issues still open](https://camo.githubusercontent.com/af847abe62fcd820c480002987a8801e02324f52f5a880f899382601824fc95c/68747470733a2f2f7777772e697369746d61696e7461696e65642e636f6d2f62616467652f6f70656e2f6d61746f6d6f2d6f72672f6465766963652d6465746563746f722e737667)](https://www.isitmaintained.com/project/matomo-org/device-detector "Percentage of issues still open")

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

[](#description)

The Universal Device Detection library that parses User Agents and Browser Client Hints to detect 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 `matomo/device-detector` to your projects requirements.

```
composer require matomo/device-detector

```

And use some code like this one:

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

use DeviceDetector\ClientHints;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;

// 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
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);

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

// Client Hints are optional
// If you want to use them your server must announce that it supports client hints, using the Accept-CH header to specify the hints that it is interested in receiving.
// See e.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/Client_hints
$clientHints = ClientHints::factory($_SERVER);

$dd = new DeviceDetector($userAgent, $clientHints);

// 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. Several cache bridges are supported:
// $dd->setCache(new DeviceDetector\Cache\DoctrineBridge(...));
// $dd->setCache(new DeviceDetector\Cache\LaravelCache(...));
// $dd->setCache(new DeviceDetector\Cache\PSR6Bridge(...));
// $dd->setCache(new DeviceDetector\Cache\PSR16Bridge(...));
// Instead of `...` provide respective cache object, for example:
// $dd->setCache(new DeviceDetector\Cache\PSR6Bridge(new Symfony\Component\Cache\Adapter\ApcuAdapter('matomo')));

// 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());
// $dd->setYamlParser(new DeviceDetector\Yaml\Pecl());

// 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();
}
```

Methods check device type:

```
$dd->isSmartphone();
$dd->isFeaturePhone();
$dd->isTablet();
$dd->isPhablet();
$dd->isConsole();
$dd->isPortableMediaPlayer();
$dd->isCarBrowser();
$dd->isTV();
$dd->isSmartDisplay();
$dd->isSmartSpeaker();
$dd->isCamera();
$dd->isWearable();
$dd->isPeripheral();
```

Methods check client type:

```
$dd->isBrowser();
$dd->isFeedReader();
$dd->isMobileApp();
$dd->isPIM();
$dd->isLibrary();
$dd->isMediaPlayer();
```

Get OS family:

```
use DeviceDetector\Parser\OperatingSystem;

$osFamily = OperatingSystem::getOsFamily($dd->getOs('name'));
```

Get browser family:

```
use DeviceDetector\Parser\Client\Browser;

$browserFamily = Browser::getBrowserFamily($dd->getClient('name'));
```

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.

```
