1. Go to this page and download the library: Download piwik/device-detector library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
piwik / device-detector example snippets
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
$clientHints = ClientHints::factory($_SERVER); // client hints are optional
$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
// $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();
}
use DeviceDetector\Parser\OperatingSystem;
$osFamily = OperatingSystem::getOsFamily($dd->getOs('name'));
use DeviceDetector\Parser\Client\Browser;
$browserFamily = Browser::getBrowserFamily($dd->getClient('name'));
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
e_once 'path/to/device-detector/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
$clientHints = ClientHints::factory($_SERVER); // client hints are optional
$dd = new DeviceDetector($userAgent, $clientHints);
// ...
// Example with PSR-6 and Symfony
$cache = new \Symfony\Component\Cache\Adapter\ApcuAdapter();
$dd->setCache(
new \DeviceDetector\Cache\PSR6Bridge($cache)
);
// Example with PSR-16 and ScrapBook
$cache = new \MatthiasMullie\Scrapbook\Psr16\SimpleCache(
new \MatthiasMullie\Scrapbook\Adapters\Apc()
);
$dd->setCache(
new \DeviceDetector\Cache\PSR16Bridge($cache)
);
// Example with Doctrine
$cache = new \Doctrine\Common\Cache\ApcuCache();
$dd->setCache(
new \DeviceDetector\Cache\DoctrineBridge($cache)
);
// Example with Laravel
$dd->setCache(
new \DeviceDetector\Cache\LaravelCache()
);