W3docs

get_browser()

The get_browser() function in PHP is used to get information about the user's browser, which is determined based on the HTTP User-Agent header that is sent by

PHP get_browser() Function

The get_browser() function in PHP is used to get information about the user's browser, which is determined based on the HTTP User-Agent header that is sent by the client's browser to the server.

Syntax

get_browser(?string $user_agent = null, bool $return_array = false): object|array|false

The get_browser() function accepts two optional parameters:

  • $user_agent: A string specifying the User-Agent to parse. If omitted (or null), the function uses the current request's User-Agent from $_SERVER['HTTP_USER_AGENT'].
  • $return_array: A boolean. If true, the function returns an associative array of browser properties. If false (the default), it returns an object with the same data as properties.

Return value: an object or array of capabilities, or false on failure.

Prerequisite: browscap.ini

get_browser() does not detect anything on its own — it looks the User-Agent up in a browser capabilities database called browscap.ini. You must download that file and point php.ini at it with the browscap directive:

; in php.ini
browscap = /path/to/browscap.ini

Without this directive set, the function returns false and emits a warning. Because the lookup table is large and must be parsed, calling get_browser() carries noticeable overhead — this is the main reason modern apps avoid it.

Returning an array

Pass true as the second argument to get an associative array. This is the easiest form to loop over or inspect with print_r():

<?php
$browser = get_browser(null, true);
print_r($browser);
?>

A typical (trimmed) result for a desktop Chrome request looks like this:

Array
(
    [browser_name_pattern] => *mozilla/5.0 (*windows nt 10.0*) applewebkit*chrome*
    [parent]    => Chrome 120.0
    [platform]  => Win10
    [browser]   => Chrome
    [version]   => 120.0
    [majorver]  => 120
    [minorver]  => 0
    [cookies]   => 1
    [javascript] => 1
    [frames]    => 1
    ...
)

Common keys include browser, version, majorver, minorver, platform, parent, cookies, javascript, frames, iframes, tables, and the regex used for matching, browser_name_pattern. Boolean-style capabilities come back as the strings "1" (true) or "0"/empty (false).

Returning an object (default)

When you omit the second argument, you get an object and read capabilities as properties:

<?php
$browser = get_browser();
echo "Browser: {$browser->browser}\n";
echo "Version: {$browser->version}\n";
echo "Platform: {$browser->platform}\n";
?>

Building a readable message

<?php
$browser = get_browser(null, true);
echo "You are using " . $browser['browser']
   . " version " . $browser['version']
   . " on " . $browser['platform'] . ".";
?>

This outputs something like You are using Chrome version 120.0 on Win10., based on the HTTP response request's User-Agent header.

Parsing a specific User-Agent

You don't have to rely on the current request — pass any User-Agent string yourself, which is handy for testing or log analysis:

<?php
$ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15";
$browser = get_browser($ua, true);
echo $browser['platform'];   // e.g. iOS
?>

Guarding against failure

Because the function returns false when browscap is not configured, always check before reading properties:

<?php
$browser = get_browser(null, true);

if ($browser === false) {
    // Fall back to the raw header
    echo $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown browser';
} else {
    echo $browser['browser'] . ' ' . $browser['version'];
}
?>

Conclusion

The get_browser() function is a useful tool for getting information about the user's browser in PHP, which can be used to optimize the user experience or for debugging purposes. It is important to note that the function depends on the User-Agent header being sent by the browser, which can be manipulated by the user or a malicious actor. Therefore, the information returned by the function should be treated as potentially unreliable and should not be relied on for security purposes. Additionally, due to performance overhead and the requirement for an external browscap.ini file, modern applications often prefer parsing the User-Agent header directly or using dedicated libraries.

  • PHP Superglobals$_SERVER, $_GET, $_POST and the other built-in arrays.
  • $_SERVER — read the raw HTTP_USER_AGENT header directly.
  • preg_match() — roll your own User-Agent parsing with regular expressions.

Practice

Practice
What is the function of get_browser() in PHP?
What is the function of get_browser() in PHP?
Was this page helpful?