PHP Luminova: Incoming Request User Agents Management
Luminova's UserAgent class is a valuable tool for devs building web applications or services that require functionality based on client devices. By providing methods to parse and extract information from...
The UserAgent Detection class is a utility class designed to parse and extract information from User-Agent strings. The User-Agent information are sent by web browsers and other clients to identify themselves to servers. This class makes it easy to work with User-Agent strings, allowing developers to extract useful information such as the browser name, version, operating system, and device type.
Examples
Use global function helper.
echo browser(null, 'instance');Get the browser name.
browser(null, 'instance')->getBrowser();Get the browser user-agent from request object.
request()->getUserAgent();
// OR
request()->agent;Initialize user agent class.
$ua = new UserAgent();
echo $ua->getBrowser();Accessing protected properties.
echo $ua->useragent;
echo $ua->browser;
echo $ua->version;
echo $ua->platform;
echo $ua->platformversion;Class Definition
- Class namespace:
Luminova\Http\UserAgent - This class implements: Luminova\Interface\LazyObjectInterface, \Stringable
Methods
constructor
Constructor
public function __construct(
?string $useragent = null,
?AbstractConfig $config = null
)Sets the User Agent and runs the compilation routine.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$useragent | string|null | The User Agent string. If not provided, it defaults to HTTP_USER_AGENT. |
$config | AbstractConfig\|null | Optional browser configuration. |
The following getters are provided through dynamic method calls.
getBrowser
Getter method for retrieving the browser information.
public function getBrowser(): stringgetUserAgent
Getter method for retrieving the user agent string.
public function getUserAgent(): stringgetPlatform
Getter method for retrieving the platform/operating system information.
public function getPlatform(): stringgetVersion
Getter method for retrieving the browser version.
public function getVersion(): stringgetRobot
Getter method for retrieving the robot name.
public function getRobot(): stringgetMobile
Getter method for retrieving the mobile device name.
public function getMobile(): stringgetReferrer
Returns the stored referrer URL after isReferral() reads the request header.
public function getReferrer(): stringgetOsVersion
Getter method for retrieving the platform/operating system version.
public function getOsVersion(): stringtoString
Get the Agent String.
public function toString(): stringReturn:
string - The Agent String.
parse
Parse the user agent string and extract browser information.This method parses the user agent string and extracts information such as browser name, version,operating system, and platform.
public static function parse(?string $userAgent = null, bool $returnArray = false): array|object|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$userAgent | string|null | The user agent string to parse. If not provided, it defaults to the user agent string from the HTTP headers. |
$returnArray | bool | If set to true, this function will return an array instead of an object. |
Return:
array<array-key, mixed>|object|false - Returns an array or object containing the parsed browser information.
replace
Parse and replace user agent class properties with new user agent information.
public function replace(string $userAgent): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$userAgent | string | The user agent string to parse and expose. |
Return:
self - Returns the current instance.
isReferral
Check if the referral hostname is from another site.
public function isReferral(?string $hostname = null, bool $includeSubdomain = false): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$hostname | string\|null | Hostname or URL to compare against. Null uses APP_HOSTNAME. |
$includeSubdomain | bool | Compare full hostnames when true. By default, compare base domains. |
Return:
bool - Return true if the referral hostname is from another site.
isRobot
Check if the user agent string is from a known robot.
public function isRobot(?string $keyword = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$keyword | string|null | Optional robot name, keyword or pattern. - Pass NULL to check if robot is in array of robot keywords Browser::$robotPatterns. |
Return:
bool - Return true if the user agent is from known robot, false otherwise.
isMobile
Check if the user agent string represents a mobile device.
public function isMobile(?string $keyword = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$keyword | string|null | Optional mobile device name, keyword or pattern. - Pass NULL to check if mobile is in array of mobile devices Browser::$mobileKeywords. |
Return:
bool - Return true if the user agent represents a mobile device, false otherwise.
isBrowser
Check if the user agent string belongs to a browser.
public function isBrowser(?string $name = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string|null | Optional browser name, keyword or pattern. If NULL is passed it will check if the user agent is any valid browser. |
Return:
bool - Return true if the user agent belongs to a specific browser, or if the given name matches the browser name or user-agent, false otherwise.
isTrusted
Check if the user agent string is trusted based on allowed browsers.
public function isTrusted(): boolReturn:
bool - Return true if the user agent matches any of the browser name / patterns in allowed browsers, false otherwise.
contains
Check if keyword or patterns matched with the user agent string, browser, mobile or robot name.
public function contains(string $pattern, ?string $property = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$pattern | string | The keyword or pattern to match. |
$property | string|null | The context to lookup matches, if null it will search user-agent string. - browser, mobile or robot |
Return:
bool - Return true if matched otherwise false.
Examples
Checking for a keyword in user agent string.
if(request()->getUserAgent()->contains('Mozilla')){
echo "YES";
}Using pattern to match only Runtime.
if(request()->getUserAgent()->contains('!(Postman)Runtime')){
echo "YES";
}reset
Reset user agent class properties.
protected function reset(): voidgetLanguage
Read a language from the parsed language list.
public function getLanguage(int $index = 0): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | Zero-based language index. |
Return:
string|null - Returns the language at the requested index, or null if unavailable.
isChromium
Check the Chromium flag set during parsing.
public function isChromium(): boolReturn:
bool - Returns true when parsing identifies a Chromium-based browser.
match
Search the full user-agent string. Attempts a regular expression match, then falls back to a case-insensitive substring search.
public function match(string $pattern, bool $asGroup = false, bool $wordBoundary = false): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$pattern | string | Pattern or text to match. |
$asGroup | bool | Wrap an undelimited pattern in a non-capturing group. |
$wordBoundary | bool | Apply word boundaries to an undelimited pattern. |
Return:
bool - Returns true on a match.
toArray
Get parsed details and classification flags. This also evaluates the current request referrer.
public function toArray(): arrayReturn:
array<string,mixed> - Returns the user agent, browser and engine details, operating system and platform details, language and attribute lists, and classification flags.
jsonSerialize
Supply the same parsed data as toArray() when the object is passed to json_encode().
public function jsonSerialize(): mixedReturn:
array<string,mixed> - Returns the parsed details and classification flags.