Important
This repository is a read-only mirror of the utopia-php monorepo. Development happens in packages/user-agent — please open issues and pull requests there.
A fast user-agent parser and device detector for PHP.
Utopia User Agent provides typed operating-system, client, device, and bot results without runtime data files or dependencies. Detection categories are lazy and memoized: asking only for a device does not run browser or bot rules.
composer require utopia-php/user-agentuse Utopia\UserAgent\UserAgent;
$agent = UserAgent::parse($_SERVER['HTTP_USER_AGENT'] ?? '');
$os = $agent->operatingSystem();
$client = $agent->client();
$device = $agent->device();
echo $os->name; // iOS
echo $client->name; // Mobile Safari
echo $device->type; // smartphone
echo $device->brand; // Apple
echo $device->model; // iPhone
if ($agent->isBot()) {
echo $agent->bot()?->name;
}Every category returns a value object. Unknown fields are null, so malformed,
empty, and unfamiliar user-agent strings are safe to inspect without exception
handling.
$os = $agent->operatingSystem();
$os->code; // ?string, for example IOS, AND, WIN, MAC
$os->name; // ?string
$os->version; // ?string
$os->isKnown(); // bool$client = $agent->client();
$client->type; // ?string: browser, library, desktop, ...
$client->code; // ?string
$client->name; // ?string
$client->version; // ?string
$client->engine; // ?string
$client->engineVersion; // ?string
$client->isBrowser(); // bool$device = $agent->device();
$device->type; // ?string: desktop, smartphone, tablet, tv, console, ...
$device->brand; // ?string
$device->model; // ?string
$device->isKnown(); // boolDevice::$type represents the device class rather than its model name.
Bot detection is independent from the other categories. A bot user-agent can still return its browser, OS, and device information.
if ($agent->isBot()) {
$agent->bot()?->name; // Googlebot
$agent->bot()?->category; // search crawler
}Each value object has a toArray() method. The complete nested result is also
available:
$data = $agent->toArray();
// [
// 'os' => ['code' => ..., 'name' => ..., 'version' => ...],
// 'client' => [...],
// 'device' => [...],
// 'bot' => null|['name' => ..., 'category' => ...],
// ]The initial rule set covers common user agents:
- Windows, Windows Phone, macOS, iOS, Android, Chrome OS, Linux, Ubuntu, Tizen, and KaiOS
- Chrome, Safari, Firefox, Edge, Opera, Samsung Internet, Android WebView, Internet Explorer, and their common mobile variants
- Common HTTP libraries
- Desktop, smartphone, tablet, TV, console, wearable, and portable-media device classes with common mobile brands and models
- Search, social-preview, monitoring, automation, SEO, and AI crawlers
Core result fields are regression-tested against Matomo DeviceDetector 6.4 for representative profiles. The runtime does not contain or load Matomo's LGPL rule data.
The hot path uses direct token checks and a small bounded set of regular expressions. There are no YAML files, runtime rule compilation, global caches, or third-party runtime dependencies.
Run the bundled comparison benchmark:
composer benchBENCH_ITERATIONS controls its duration. Matomo DeviceDetector is installed
only as a development dependency for differential tests and benchmarks.
There is no skipBotDetection() setting. Bot matching runs only when bot()
or isBot() is requested and never prevents the other categories from being
detected.