BrowserDetector
in package
BrowserDetector
- Uses native get_browser() when available and configured (browscap).
- Falls back to a lightweight User-Agent parser when get_browser is unavailable.
- Safe: does not emit warnings/errors if browscap/get_browser is missing.
Comments in English inside the file.
Table of Contents
Properties
- $data : array<string, mixed>
- $userAgent : string
Methods
- __construct() : mixed
- __destruct() : mixed
- __get() : mixed
- __isset() : bool
- __toString() : string
- debugDump() : array<string|int, mixed>
- Simple debug print (array).
- get() : mixed
- Get named property or $default if not present.
- getBrowscapPath() : string|null
- Return the value of browscap ini setting (path) or null if not configured.
- getUserAgent() : string
- Return raw user agent string used.
- isBot() : bool
- Check if UA is known crawler/bot
- isBrowser() : bool
- Check if browser name matches (case-insensitive).
- isBrowserVersion() : bool
- Check whether current browser version is >= (or other operator) given version.
- isBrowserVersionAtLeast() : bool
- Convenience: is version at least
- isGetBrowserAvailable() : bool
- Check if native get_browser exists and browscap is configured (non-empty).
- isMobile() : bool
- Check if device is mobile (heuristic)
- isPlatform() : bool
- Check platform equality/containment (case-insensitive)
- refresh() : void
- Force re-evaluation (e.g. after changing UA) - you can pass new UA.
- summary() : string
- Return a human readable summary.
- supports() : bool
- Generic capability check: see if header/property is present and truthy.
- toArray() : array<string, mixed>
- Get all detected properties as array.
- detectNameFromUA() : string
- Detect browser name from User-Agent (fallback helper).
- normalizeGetBrowserArray() : array<string, mixed>
- Normalize array returned by get_browser() (string/true/false conversions).
- useFallbackParser() : void
- Lightweight fallback parser for common browsers and platforms.
- useGetBrowser() : void
- Attempt to populate $this->data using native get_browser().
Properties
$data
private
array<string, mixed>
$data
= []
$userAgent
private
string
$userAgent
Methods
__construct()
public
__construct([string|null $userAgent = null ][, bool $preferGetBrowser = true ]) : mixed
Parameters
- $userAgent : string|null = null
-
if null, will use $_SERVER['HTTP_USER_AGENT'] or empty string
- $preferGetBrowser : bool = true
-
try get_browser when available (default true)
__destruct()
public
__destruct() : mixed
__get()
public
__get(mixed $name) : mixed
Parameters
- $name : mixed
__isset()
public
__isset(mixed $name) : bool
Parameters
- $name : mixed
Return values
bool__toString()
public
__toString() : string
Return values
stringdebugDump()
Simple debug print (array).
public
debugDump() : array<string|int, mixed>
Return values
array<string|int, mixed>get()
Get named property or $default if not present.
public
get(string $name[, mixed $default = null ]) : mixed
Common keys (when get_browser used): browser, version, platform, javascript, cookies, cssversion, crawler, ismobiledevice
Parameters
- $name : string
- $default : mixed = null
getBrowscapPath()
Return the value of browscap ini setting (path) or null if not configured.
public
static getBrowscapPath() : string|null
Return values
string|nullgetUserAgent()
Return raw user agent string used.
public
getUserAgent() : string
Return values
stringisBot()
Check if UA is known crawler/bot
public
isBot() : bool
Return values
boolisBrowser()
Check if browser name matches (case-insensitive).
public
isBrowser(string $name) : bool
Accepts partial matches: e.g. isBrowser('Chrome'), isBrowser('Chrom')
Parameters
- $name : string
Return values
boolisBrowserVersion()
Check whether current browser version is >= (or other operator) given version.
public
isBrowserVersion(string $browserName, string $version[, string $operator = '>=' ]) : bool
Example: isBrowserVersionAtLeast('Chrome', '80') -> bool
Parameters
- $browserName : string
-
check only if name matches (partial, case-insensitive)
- $version : string
-
version to compare against
- $operator : string = '>='
-
any operator supported by version_compare (>, >=, <, <=, ==, !=)
Return values
boolisBrowserVersionAtLeast()
Convenience: is version at least
public
isBrowserVersionAtLeast(string $browserName, string $version) : bool
Parameters
- $browserName : string
- $version : string
Return values
boolisGetBrowserAvailable()
Check if native get_browser exists and browscap is configured (non-empty).
public
static isGetBrowserAvailable() : bool
Return values
boolisMobile()
Check if device is mobile (heuristic)
public
isMobile() : bool
Return values
boolisPlatform()
Check platform equality/containment (case-insensitive)
public
isPlatform(string $platform) : bool
Parameters
- $platform : string
Return values
boolrefresh()
Force re-evaluation (e.g. after changing UA) - you can pass new UA.
public
refresh([string|null $userAgent = null ][, bool $preferGetBrowser = true ]) : void
Parameters
- $userAgent : string|null = null
- $preferGetBrowser : bool = true
summary()
Return a human readable summary.
public
summary() : string
Return values
stringsupports()
Generic capability check: see if header/property is present and truthy.
public
supports(string $capability) : bool
Example: supports('javascript'), supports('cookies'), supports('iframes')
Parameters
- $capability : string
Return values
booltoArray()
Get all detected properties as array.
public
toArray() : array<string, mixed>
Return values
array<string, mixed>detectNameFromUA()
Detect browser name from User-Agent (fallback helper).
private
detectNameFromUA() : string
Return values
stringnormalizeGetBrowserArray()
Normalize array returned by get_browser() (string/true/false conversions).
private
normalizeGetBrowserArray(array<string, mixed> $arr) : array<string, mixed>
Parameters
- $arr : array<string, mixed>
Return values
array<string, mixed>useFallbackParser()
Lightweight fallback parser for common browsers and platforms.
private
useFallbackParser() : void
This parser intentionally focuses on practical checks (name/version/platform/mobile/bot).
useGetBrowser()
Attempt to populate $this->data using native get_browser().
private
useGetBrowser() : void
Use @ to suppress warnings if browscap is misconfigured; validate result.