get_browser() function in PHP

The get_browser() function looks up the user's browscap.ini file and returns the capabilities of the user's browser. This function requires the browscap configuration setting in php.ini to be properly configured.

Syntax

get_browser(user_agent, return_array)

Parameters

  • user_agent − Optional. The name of HTTP user agent. If omitted, the current user agent is used.

  • return_array − Optional. If set to true, the function returns an array instead of an object.

Return Value

The get_browser() function returns an object or array containing information about the user's browser capabilities, or FALSE on failure.

Basic Usage

Here's how to use get_browser() to get browser information −

<?php
   echo $_SERVER['HTTP_USER_AGENT'] . "<br>";
   $browserInfo = get_browser();
   print_r($browserInfo);
?>

Returning as Array

You can also get the browser information as an array −

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

Custom User Agent

You can check capabilities of a specific user agent −

<?php
   $userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X)";
   $browserInfo = get_browser($userAgent, true);
   print_r($browserInfo);
?>

Output

The typical output might look like this −

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0.4472.124 Safari/537.36
Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows nt 10\.0; win64; x64) applewebkit/.* chrome/.*safari/.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/* Chrome/*Safari/*
    [parent] => Chrome 91.0
    [comment] => Chrome 91.0
    [browser] => Chrome
    [version] => 91.0
    [majorver] => 91
    [minorver] => 0
    [platform] => Win10
    [alpha] => 
    [beta] => 
    [cookies] => 1
    [javascript] => 1
)

Configuration Requirements

The get_browser() function requires the browscap.ini file to be configured in php.ini. The browscap directive must point to a valid browscap.ini file, or the function will return FALSE. You can download the latest browscap.ini from the official browscap project website.

; Example php.ini configuration
browscap = /path/to/browscap.ini

Key Properties

Property Description
browser Browser name (e.g., Chrome, Firefox)
version Browser version number
platform Operating system (e.g., Win10, Linux)
javascript JavaScript support (1 or 0)
cookies Cookie support (1 or 0)

Conclusion

The get_browser() function provides detailed browser capability information but requires proper browscap.ini configuration. It's useful for browser detection and capability checking in web applications, though modern alternatives like JavaScript feature detection are often preferred.

Updated on: 2026-03-15T08:03:06+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements