0

I am trying to use PHP to detect which version of a browser is being used. so far I have managed to find out which browser is being used but not which version of that browser can anyone suggest what I need to be able to find this out

Here is what i have so far. Thanks

    <td><input type="hidden" name="browser" value="<?php  
    $user_agent                = $_SERVER['HTTP_USER_AGENT'];

    if (preg_match('/MSIE/i', $user_agent)) { 
    echo "Internet Explorer";
    /*} else {
    echo "Not IE";
    */}

    if (preg_match('/Firefox/i', $user_agent)) { 
    echo "Firefox";
    /*} else {
    echo "Not Firefox";
    */} 

    if (preg_match('/Chrome/i', $user_agent)) { 
    echo "Google Chrome";
    } elseif (preg_match('/Safari/i', $user_agent)) {
    echo "Safari";
    }

    ?>"/></td>
GDP227
  • 3
  • 1
  • 2

5 Answers5

0

try get_browser()

$browser = get_browser(null, true);
echo $browser['browser'];
echo $browser['version'];

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

Also follow to enable in php.ini if get_browser showing warning

browscap ini directive not set

How should I be setting browscap.ini file

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • Okay I tried using this, I changed the input to text so I could see what happened and in the box was this message: Warning: get_browser(): browscap ini directive not set in /home/trebleclick/webapps/mafanakidsupgrade/catalog/view/theme/default/template/account/register.tpl on line 116 – GDP227 Jul 23 '14 at 11:44
  • try http://stackoverflow.com/questions/2036956/browscap-ini-directive-not-set and for http://stackoverflow.com/questions/12663304/how-should-i-be-setting-browscap-ini-file – Rakesh Sharma Jul 23 '14 at 11:46
0

You can use get_browser. Example from PHP doc:

$browser = get_browser(null, true);
print_r($browser);

Result in:

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
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
)
Debflav
  • 1,131
  • 7
  • 17
0

You can detect the browser by using Browser class download it from Github

Configuration

      include(/your-path/Browser.php);
$browser = new Browser();
        if( $browser->getBrowser() == Browser::BROWSER_IE && $browser->getVersion() >= 8 ) 
        {
            echo "Your browser is Internet explorer version 8";                                                                                                                                    
    }

similarly you can check all the browsers.

Manju
  • 747
  • 4
  • 10
  • 21
0

the best way to do this is as follows $browser = get_browser();

Note that this returns an object not a string. So to access the browser information, you could do this $brw=$browser->browser; echo $brw;

I hope this helps.

-1

you can use the below code to find the browser + version this is html and javascript

<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title> test</title>

      <script type='text/javascript' src='http://code.jquery.com/jquery-2.0.0.js'></script>

      <style type='text/css'>

      </style>         


    <script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){
    var _browser = {};

    function detectBrowser() {
        var uagent = navigator.userAgent.toLowerCase();
        $("#result").html("User agent string: <b>" + uagent + "</b>");

        _browser.opera = /mozilla/.test(uagent) && /applewebkit/.test(uagent) && /chrome/.test(uagent) && /safari/.test(uagent) && /opr/.test(uagent);
        _browser.safari = /applewebkit/.test(uagent) && /safari/.test(uagent) && !/chrome/.test(uagent);
        _browser.firefox = /mozilla/.test(uagent) && /firefox/.test(uagent);
        _browser.chrome = /webkit/.test(uagent) && /chrome/.test(uagent);
        _browser.msie = /msie/.test(uagent);
        _browser.version = '';

        for (x in _browser)
        {
            if (_browser[x]) {
                if (x !== "opera") {
                    _browser.version = uagent.match(new RegExp("(" + x + ")( |/)([0-9]+)"))[3];
                    $("#result").append("<br/>The browser is " + x + " " + _browser.version);
                }
                else {
                    _browser.version = uagent.match(new RegExp("(opr)( |/)([0-9]+)"))[3];
                    $("#result").append("<br/>The browser is " + x + " " + _browser.version);    
                }
                break;
            }
        }

    }



    detectBrowser();


    });//]]>  

    </script>


    </head>
    <body>
      <div id="result"></div>

    </body>


    </html>
Ajit Singh
  • 1,132
  • 1
  • 13
  • 24