Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP get_browser() Function

  • Last Updated : 03 Dec, 2021

In this article, we will know to check whether the user’s browser capability using the get_browser() function in PHP, along with understanding its implementation through the example. The get_browser() function in PHP is an inbuilt function that is used to tell the user about the browser’s capabilities. This function looks up the user’s browscap.ini file and returns the capabilities of the user’s browser. The user_agent and the return_array are passed as parameters to the get_browser() function and it returns an object or an array with information about the user’s browser on success, or FALSE on failure.

Syntax:

get_browser(user_agent, return_array)

Parameters Used: The get_browser() function in PHP accepts two parameters:

  • user_agent: It is an optional parameter that specifies the name of an HTTP user agent. Default is the value of $HTTP_USER_AGENT.
  • return_array: It is an optional parameter that returns an array instead of an object if it is set to True.

Return Value: It returns an object or an array with information about the user’s browser on success, or FALSE on failure.

Exceptions:

  • The user_agent parameter can be bypassed with a NULL value.
  • The cookies value simply means that the browser itself is capable of accepting cookies and does not mean the user has enabled the browser to accept cookies or not.
  • In order for this function to work the browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

Approach: For checking the browser capability of the users’ system & acknowledging them accordingly, we will be using the  get_browser() function that contains the 2 parameters namely, user_agent that will be utilized to specify the name of an HTTP user agent, & the second parameter is return_array that will return an array instead of an object if the value is set to true.

Example 1: The below example illustrate the get_browser() function that will display the user’s browser capability.

PHP




<?php
  echo $_SERVER['HTTP_USER_AGENT'];
 
  // Using get_browser() to display
  // capabilities of the user browser
  $mybrowser = get_browser();
  print_r($mybrowser);
?>

Output:

[parent] => IE 6.0
[platform] => WinXP
[netclr] => 1
[browser] => IE
[version] => 6
[majorver] => 6
[minorver] => 0
 => 2
[frames] => 1
[iframes] => 1

Example 2: The below example illustrate the get_browser() function with the return array that is set to true.

PHP




<?php
    echo $_SERVER['HTTP_USER_AGENT'];
 
    // Using get_browser() with return_array set to TRUE
    $mybrowser = get_browser(null, true);
    print_r($mybrowser);
?>

Output:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .;
        windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?;
                Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
     => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [beta] => 1
) 

Reference: http://php.net/manual/en/function.get-browser.php


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!