agent
👮 A PHP desktop/mobile user agent parser with support for Laravel, based on Mobiledetect
Top Related Projects
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Browser sniffing gone too far — A useragent parser library for PHP
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
Quick Overview
Jenssegers/agent is a PHP library for detecting and parsing user agent strings. It provides an easy-to-use interface for extracting information about browsers, operating systems, and devices from user agent data. This library is particularly useful for web developers who need to tailor their applications based on client information.
Pros
- Easy to use with a simple and intuitive API
- Supports a wide range of browsers, operating systems, and devices
- Regularly updated to keep up with new user agent strings
- Lightweight and has minimal dependencies
Cons
- May not be 100% accurate for all user agent strings, especially for less common or newer devices
- Requires periodic updates to maintain accuracy as new browsers and devices are released
- Limited to PHP environments, not suitable for other programming languages
Code Examples
- Detecting browser information:
$agent = new \Jenssegers\Agent\Agent();
$browser = $agent->browser();
$version = $agent->version($browser);
echo "Browser: $browser $version";
- Checking for mobile devices:
$agent = new \Jenssegers\Agent\Agent();
if ($agent->isMobile()) {
echo "This is a mobile device";
} elseif ($agent->isTablet()) {
echo "This is a tablet device";
} else {
echo "This is a desktop device";
}
- Getting operating system information:
$agent = new \Jenssegers\Agent\Agent();
$platform = $agent->platform();
$version = $agent->version($platform);
echo "Operating System: $platform $version";
Getting Started
To use jenssegers/agent in your PHP project, follow these steps:
- Install the library using Composer:
composer require jenssegers/agent
- Include the Composer autoloader in your PHP file:
require 'vendor/autoload.php';
- Create an instance of the Agent class and start using it:
use Jenssegers\Agent\Agent;
$agent = new Agent();
// Now you can use various methods to get information
$browser = $agent->browser();
$platform = $agent->platform();
$device = $agent->device();
// Check for specific devices or browsers
$isChrome = $agent->isChrome();
$isMobile = $agent->isMobile();
With these steps, you can quickly integrate jenssegers/agent into your PHP project and start detecting user agent information.
Competitor Comparisons
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Pros of Device Detector
- More comprehensive device detection, including IoT devices and bots
- Regular updates to its device database, ensuring up-to-date detection
- Supports multiple programming languages through ports
Cons of Device Detector
- Larger codebase and database, potentially impacting performance
- Steeper learning curve due to more complex API and configuration options
Code Comparison
Agent:
$agent = new Agent();
$browser = $agent->browser();
$platform = $agent->platform();
Device Detector:
$dd = new DeviceDetector($userAgent);
$dd->parse();
$clientInfo = $dd->getClient();
$osInfo = $dd->getOs();
$device = $dd->getDeviceName();
Summary
Agent is a lightweight and easy-to-use library for basic user agent parsing, while Device Detector offers more comprehensive device detection capabilities. Agent is suitable for simple use cases, whereas Device Detector is better for applications requiring detailed device information. The choice between the two depends on the specific needs of your project, balancing between simplicity and feature richness.
Browser sniffing gone too far — A useragent parser library for PHP
Pros of Parser-PHP
- More comprehensive device and browser detection
- Regular updates and maintenance
- Extensive documentation and examples
Cons of Parser-PHP
- Larger file size and potentially slower performance
- More complex setup and usage
- Steeper learning curve for beginners
Code Comparison
Parser-PHP:
use WhichBrowser\Parser;
$result = new Parser($_SERVER['HTTP_USER_AGENT']);
echo $result->browser->name;
echo $result->device->type;
Agent:
use Jenssegers\Agent\Agent;
$agent = new Agent();
echo $agent->browser();
echo $agent->device();
Both libraries provide similar functionality for detecting user agents, browsers, and devices. Parser-PHP offers more detailed information but requires more setup and resources. Agent is simpler to use and lighter weight but may not provide as much depth in its detection capabilities.
Parser-PHP is better suited for projects requiring in-depth device and browser information, while Agent is ideal for simpler use cases and projects with performance constraints. The choice between the two depends on the specific needs of your project and the level of detail required in user agent parsing.
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
Pros of Mobile-Detect
- More focused on mobile device detection, potentially offering more accurate results for mobile-specific use cases
- Lighter weight and simpler to use for basic mobile detection tasks
- Regularly updated device detection rules
Cons of Mobile-Detect
- Limited to mobile device detection, lacking broader user agent parsing capabilities
- Less flexible for general-purpose user agent analysis
- May require more manual updates to stay current with new devices
Code Comparison
Mobile-Detect:
$detect = new Mobile_Detect;
if ($detect->isMobile()) {
echo 'Mobile device detected';
}
Agent:
$agent = new Agent();
if ($agent->isMobile()) {
echo 'Mobile device detected';
}
echo $agent->platform() . ' ' . $agent->version($agent->platform());
Agent provides more comprehensive user agent parsing, including platform and version information, while Mobile-Detect focuses primarily on mobile device detection. Agent offers a more versatile solution for general user agent analysis, whereas Mobile-Detect excels in specific mobile detection scenarios. The choice between the two depends on the project's requirements and the depth of user agent information needed.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Agent
A PHP desktop/mobile user agent parser with support for Laravel, based on Mobile Detect with desktop support and additional functionality.
Installation
Install using composer:
composer require jenssegers/agent
Laravel (optional)
Add the service provider in config/app.php
:
Jenssegers\Agent\AgentServiceProvider::class,
And add the Agent alias to config/app.php
:
'Agent' => Jenssegers\Agent\Facades\Agent::class,
Basic Usage
Start by creating an Agent
instance (or use the Agent
Facade if you are using Laravel):
use Jenssegers\Agent\Agent;
$agent = new Agent();
If you want to parse user agents other than the current request in CLI scripts for example, you can use the setUserAgent
and setHttpHeaders
methods:
$agent->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2');
$agent->setHttpHeaders($headers);
All of the original Mobile Detect methods are still available, check out some original examples at https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples
Is?
Check for a certain property in the user agent.
$agent->is('Windows');
$agent->is('Firefox');
$agent->is('iPhone');
$agent->is('OS X');
Magic is-method
Magic method that does the same as the previous is()
method:
$agent->isAndroidOS();
$agent->isNexus();
$agent->isSafari();
Mobile detection
Check for mobile device:
$agent->isMobile();
$agent->isTablet();
Match user agent
Search the user agent with a regular expression:
$agent->match('regexp');
Additional Functionality
Accept languages
Get the browser's accept languages. Example:
$languages = $agent->languages();
// ['nl-nl', 'nl', 'en-us', 'en']
Device name
Get the device name, if mobile. (iPhone, Nexus, AsusTablet, ...)
$device = $agent->device();
Operating system name
Get the operating system. (Ubuntu, Windows, OS X, ...)
$platform = $agent->platform();
Browser name
Get the browser name. (Chrome, IE, Safari, Firefox, ...)
$browser = $agent->browser();
Desktop detection
Check if the user is using a desktop device.
$agent->isDesktop();
This checks if a user is not a mobile device, tablet or robot.
Phone detection
Check if the user is using a phone device.
$agent->isPhone();
Robot detection
Check if the user is a robot. This uses jaybizzle/crawler-detect to do the actual robot detection.
$agent->isRobot();
Robot name
Get the robot name.
$robot = $agent->robot();
Browser/platform version
MobileDetect recently added a version
method that can get the version number for components. To get the browser or platform version you can use:
$browser = $agent->browser();
$version = $agent->version($browser);
$platform = $agent->platform();
$version = $agent->version($platform);
Note, the version method is still in beta, so it might not return the correct result.
License
Laravel User Agent is licensed under The MIT License (MIT).
Security contact information
To report a security vulnerability, follow these steps.
Top Related Projects
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Browser sniffing gone too far — A useragent parser library for PHP
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot