Install jenssegers/agent via Composer
masterInstall the package using Composer to add it to your PHP project.
composer require jenssegers/agentrepository·master·Indexed 26 days ago
https://github.com/jenssegers/agentA PHP user agent parser providing desktop and mobile detection. It extends Mobile Detect to include desktop support and metadata extraction for browser/platform versions, device types (phone, tablet, desktop), and accepted languages. Includes integration for Laravel via a Service Provider and Facade, and utilizes jaybizzle/crawler-detect for robot and crawler identification.
Install the package using Composer to add it to your PHP project.
composer require jenssegers/agentconfig/app.php.Create a new instance of the Agent class to begin parsing user agents. If you are using Laravel, you can use the Agent Facade instead.
use Jenssegers\Agent\Agent;
$agent = new Agent();Jenssegers\Agent\AgentServiceProvider in your config/app.php providers array. The package is registered as a singleton under the key agent and is aliased to the Jenssegers\Agent\Agent class. It is a deferred provider, meaning it will only be loaded when the agent service is actually requested.You can check for specific properties (OS, Browser, Device) using the is() method or via magic is{Property}() methods.
Using is():
$agent->is('Windows');
$agent->is('Firefox');
$agent->is('iPhone');
$agent->is('OS X');Using magic methods:
$agent->isAndroidOS();
$agent->isNexus();
$agent->isSafari();Identify if the user agent belongs to a robot using isRobot(). This functionality utilizes jaybizzle/crawler-detect internally.
$agent->isRobot();
$robot = $agent->robot();Search the user agent string using a regular expression with the match() method.
$agent->match('regexp');Retrieve the version number for a specific browser or platform using the version() method. Note that this method is currently in beta and may not always return accurate results.
$browser = $agent->browser();
$version = $agent->version($browser);
$platform = $agent->platform();
$version = $agent->version($platform);Use the following methods to identify the device category:
$agent->isMobile(): Returns true if the device is mobile.$agent->isTablet(): Returns true if the device is a tablet.$agent->isDesktop(): Returns true if the user is not using a mobile device, tablet, or robot.$agent->isPhone(): Returns true if the user is using a phone device.In environments like CLI scripts where the current request is not available, use setUserAgent() and setHttpHeaders() to parse specific user agent strings and headers.
$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);Extract specific metadata from the user agent:
$agent->device(): Returns the device name (e.g., iPhone, Nexus, AsusTablet) if mobile.$agent->platform(): Returns the operating system name (e.g., Ubuntu, Windows, OS X).$agent->browser(): Returns the browser name (e.g., Chrome, IE, Safari, Firefox).$agent->robot(): Returns the name of the detected robot.$agent->languages(): Returns an array of the browser's accepted languages.The Agent class integrates with CrawlerDetect to identify search engine crawlers and other bots.
isRobot([$userAgent]): Returns true if the User Agent is a known crawler.robot([$userAgent]): Returns the name of the detected robot (e.g., 'Googlebot') if it is a crawler, otherwise returns false.getCrawlerDetect(): Returns the underlying CrawlerDetect instance.