Identify browser and its version

Asked

Viewed 10,221 times

5

I did several searches on different sites but I have not found a definitive solution and that for sure for all browsers

I wonder how I can identify the browser and its version using PHP.

For example:

IE 9
CHROME 39
  • 2

    $_SERVER['HTTP_USER_AGENT']

  • @gmsantos yes, I’ve done a lot of research on this but it hasn’t solved the problem yet.

  • 1

    @Silvioandorinha I still do not understand, if this is not your problem, explain better what you would like.... what is your need? If you are going to discover the browser and its version, this is the only way.... there is something else you need?

  • In the documentation you find a code that does what you are asking. Here is the example link. Link.

  • @Marcelobonifazio yes, I wonder if anyone knows any library that does this because I don’t know much of it and to do it would take a lot of work and need to do it fast

  • @Silvioandorinha unfortunately does not have much option, if you want to implement something like this on your page, you have to understand at least the least of the thing....

Show 1 more comment

3 answers

9


The information about the client’s browser is in the variable $_SERVER['HTTP_USER_AGENT']. Other details can be found by the function get_browser().

When using the $_SERVER['HTTP_USER_AGENT'] we should note that it is not in a language of easy understanding, it is necessary to translate the User Agents to extract that information.

This user agent analysis can be done manually or you can use a package to help you with it. I found this which is very simple to use.

After installed via Poser:

composer require sinergi/browser-detector

just do the following:

use Sinergi\BrowserDetector\Browser;

$browser = new Browser();

if ($browser->getName() === $browser::CHROME) {
    echo 'Por favor, troque seu browser';
}

On github itself we have documentation on how to use this package.

6

Hello, I chose to work as follows:

I created a function to check the browser and the OS

private function VerificaNavegadorSO() {
    $ip = $_SERVER['REMOTE_ADDR'];

    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'Linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'Mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'Windows';
    }


    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
    {
        $bname = 'Internet Explorer';
        $ub = "MSIE";
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
        $bname = 'Mozilla Firefox';
        $ub = "Firefox";
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
        $bname = 'Google Chrome';
        $ub = "Chrome";
    }
    elseif(preg_match('/AppleWebKit/i',$u_agent))
    {
        $bname = 'AppleWebKit';
        $ub = "Opera";
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
        $bname = 'Apple Safari';
        $ub = "Safari";
    }

    elseif(preg_match('/Netscape/i',$u_agent))
    {
        $bname = 'Netscape';
        $ub = "Netscape";
    }

    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
    }


    $i = count($matches['browser']);
    if ($i != 1) {
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }

    // check if we have a number
    if ($version==null || $version=="") {$version="?";}

    $Browser = array(
            'userAgent' => $u_agent,
            'name'      => $bname,
            'version'   => $version,
            'platform'  => $platform,
            'pattern'    => $pattern
    );

    $navegador = "Navegador: " . $Browser['name'] . " " . $Browser['version'];
    $so = "SO: " . $Browser['platform'];

    /* Para finalizar coloquei aqui o meu insert para salvar na base de dados... Não fiz nada para mostrar em tela, pois só uso para fins de log do sistema  */
}

In the table it saves "Browser: Google Chrome 39.0.2171.95 - OS: Windows"

  • Perfect! but only has a little problem when I test with internet explorer it gives as undefied UB variable

  • Right, but did you come to debug to see at what point the error occurs? In the system I implanted was tested with IE and works normally.

  • Yes debugged, probably it should not have entered the if when checking if it is the IE

  • 1

    Here worked just right the function of @Isabela Silvio

  • @Marcelobonifazio is giving this error http://s4.postimg.org/qyekb6ypp/Capturar.png

  • Silvio, Post all your code to help (preferably in new topic, since the solution was presented and the problem is now in your code), because if the function is ok for me and @Marcelobonifazio must be something particular of your code.

Show 1 more comment

1

Check the User Agent from the browser, and do the treatment according to the result, you can discover this via server which received the reply request, and in the reply already do the processing of the page to be rendered, or via client, and render in the client itself.

PHP

$_SERVER['HTTP_USER_AGENT'];

JS

function UserAgente(){
      var ua = window.navigator.userAgent;
      return ua;
}

With this do a compatibility check according to the version of the browser used by the client....

Here you can find a complete list of User Agents

Dai just work with Expressões Regulares. Identifying each version of a browser by your User Agent, this is quite laborious. Remembering that there are hundreds of browsers, each with dozens and dozens of different versions.

  • He wants to know the browser version, not php

  • Yes, I understood by rereading the question, I had even edited the topic of his question to be clearer "Identify browser and its version via PHP"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.