Luminova Framework

PHP Luminova: Incoming Request User Agents Management

Last updated: 2026-11-28 20:57:12

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


Methods

constructor

Constructor

public function __construct(
    ?string $useragent = null,
    ?AbstractConfig $config = null
)

Sets the User Agent and runs the compilation routine.

Parameters:

ParameterTypeDescription
$useragentstring|nullThe User Agent string. If not provided, it defaults to HTTP_USER_AGENT.
$configAbstractConfig\|nullOptional browser configuration.

The following getters are provided through dynamic method calls.

getBrowser

Getter method for retrieving the browser information.

public function getBrowser(): string

getUserAgent

Getter method for retrieving the user agent string.

public function getUserAgent(): string

getPlatform

Getter method for retrieving the platform/operating system information.

public function getPlatform(): string

getVersion

Getter method for retrieving the browser version.

public function getVersion(): string

getRobot

Getter method for retrieving the robot name.

public function getRobot(): string

getMobile

Getter method for retrieving the mobile device name.

public function getMobile(): string

getReferrer

Returns the stored referrer URL after isReferral() reads the request header.

public function getReferrer(): string

getOsVersion

Getter method for retrieving the platform/operating system version.

public function getOsVersion(): string

toString

Get the Agent String.

public function toString(): string

Return:

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|bool

Parameters:

ParameterTypeDescription
$userAgentstring|nullThe user agent string to parse. If not provided, it defaults to the
user agent string from the HTTP headers.
$returnArrayboolIf 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): self

Parameters:

ParameterTypeDescription
$userAgentstringThe 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): bool

Parameters:

ParameterTypeDescription
$hostnamestring\|nullHostname or URL to compare against. Null uses APP_HOSTNAME.
$includeSubdomainboolCompare 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): bool

Parameters:

ParameterTypeDescription
$keywordstring|nullOptional 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): bool

Parameters:

ParameterTypeDescription
$keywordstring|nullOptional 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): bool

Parameters:

ParameterTypeDescription
$namestring|nullOptional 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(): bool

Return:

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): bool

Parameters:

ParameterTypeDescription
$patternstringThe keyword or pattern to match.
$propertystring|nullThe 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(): void

getLanguage

Read a language from the parsed language list.

public function getLanguage(int $index = 0): ?string

Parameters:

ParameterTypeDescription
$indexintZero-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(): bool

Return:

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): bool

Parameters:

ParameterTypeDescription
$patternstringPattern or text to match.
$asGroupboolWrap an undelimited pattern in a non-capturing group.
$wordBoundaryboolApply 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(): array

Return:

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(): mixed

Return:

array<string,mixed> - Returns the parsed details and classification flags.