PHP | get_browser() Function

The get_browser() function in PHP is an inbuilt function which is used to tell the user about the browser’s capabilities.
The get_browser() 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.



  1. user_agent : It is an optional parameter which specifies the name of an HTTP user agent. Default is the value of $HTTP_USER_AGENT.
  2. return_array : It is an optional parameter which 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:

  1. The user_agent parameter can be bypassed with a NULL value.
  2. 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.
  3. 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.

Below programs illustrate the get_browser() function:

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 Posts:



    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.