PHP Luminova: Browser's UserAgent Detection Configuration
Customize the browser, mobile device, and crawler patterns used to classify user agents.
The Browser configuration defines the patterns and keywords used to identify robots, mobile devices, and trusted browsers from user agent strings. Adjust it to control how Http UserAgent Detection classifies clients in your application.
Purpose
The Browser class centralizes the detection lists used by Luminova\Http\UserAgent. When you create a UserAgent instance without passing a configuration object, it loads \App\Config\Browser automatically. The lists are read by these methods:
| Method | Uses |
|---|---|
UserAgent::isRobot() | $robotPatterns |
UserAgent::isMobile() | $mobileKeywords |
UserAgent::isTrusted() | $browsers |
isRobot() and isMobile() are also evaluated when a UserAgent instance is created or when UserAgent::replace() is called, so changes to these lists affect every instance.
Class Definition
- Full namespace:
App\Config\Browser - File path:
/app/Config/Browser.php - This class extends Luminova\Base\AbstractConfig.
- This class is final.
Properties
robotPatterns
Define a list of known robot patterns and their names.
An associative array where the key is a robot keyword or pattern found in the user agent string, and the value is the robot name.
/**
* @var array<string, string>
*/
public array $robotPatterns = [
'Googlebot' => 'Googlebot',
//...
];Each key is tested against the raw user agent string in order, and the first match wins. A key can be a plain keyword (matched case-insensitively), a pipe separated list such as 'bingbot|msnbot', or a slash delimited regex such as '/bot\b/i'. The matched value is stored as the robot name and returned by getRobot().
If no key matches, the parser's own bot detection is used as a fallback and the robot name is set to unknown.
mobileKeywords
Define a list of known mobile device keywords and their names.
An associative array where the key is a keyword found in mobile device user agents, and the value is the device name.
/**
* @var array<string, string>
*/
public array $mobileKeywords = [
'android' => 'Android',
//...
];Keys are matched as case-insensitive plain substrings against the raw user agent string. Regex patterns are not supported here. The first match wins, and the value is stored as the mobile name returned by getMobile().
If no key matches, the user agent is still treated as mobile when it contains mobile, android, tablet, ipad, or ipod, and the mobile name is set to unknown.
Note:The order is important.Always put the known mobile first before
Generic Mobile.
browsers
Define a list of known browser keywords or patterns.
An array of identifiers representing common keywords or patterns found in browser user agents.
/**
* @var string[]
*/
public array $browsers = [
'Chrome',
//...
];Use Case:This is used when you call
Luminova\Http\UserAgent::isTrusted().
isTrusted() returns:
falseif the user agent string is empty.trueif$browsersis empty, so every non empty user agent is trusted.trueif the parsed browser name exists as an array key in$browsers.trueif any value in$browsersmatches the raw user agent string. Values are matched case-insensitively and can be a keyword or a regex pattern.falseotherwise.
This is useful in a hybrid mobile app, where you can add your webview's identifier to $browsers and use isTrusted() to filter out other clients.
Note:The User-Agent header is set by the client and can be spoofed. Treat
isTrusted()as a basic filter, not as authentication.
Example
Pass a configuration object explicitly, or let UserAgent create Browser for you.
use Luminova\Http\UserAgent;
$ua = new UserAgent();
if ($ua->isRobot()) {
echo $ua->getRobot(); // e.g. "Googlebot"
}
if ($ua->isMobile()) {
echo $ua->getMobile(); // e.g. "Android"
}
if (!$ua->isTrusted()) {
// Reject or limit the request
}