Identify browser and its version

5

I did several searches on different sites but I still did not find a definitive solution and that right for all browsers

I would like to know how I can identify the browser and its version using PHP.

For example:

IE 9
CHROME 39
    
asked by anonymous 14.01.2015 / 14:20

3 answers

8

Information about the client browser is found in the $_SERVER['HTTP_USER_AGENT'] variable. Further details can be found by get_browser() .

When using $_SERVER['HTTP_USER_AGENT'] we should note that it is not in an easy-to-understand language, we need to translate < in> User Agents to extract this information.

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

Once installed via composer:

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';
}

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

    
14.01.2015 / 14:33
6

Hello, I chose to work as follows:

I created a function to check the browser and 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"

    
14.01.2015 / 14:39
1

Check the User Agent of the browser, and do the treatment according to the result, you can find out this via server that received the response request, and in the answer already do the page handling client , and render on the client itself.

PHP

$_SERVER['HTTP_USER_AGENT'];

JS

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

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

Here you can find a complete list of User Agents

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

    
14.01.2015 / 14:23