Check user's browser in PHP

8

How do I check if the user is using the Internet Explorer browser, version 10 down, so that I can display a message to update it before entering my site?

    
asked by anonymous 15.08.2014 / 17:48

7 answers

0

I used the following code:

/* VERIFICAR NAVEGADOR */
function Navegador() {

    $MSIE = strpos($_SERVER['HTTP_USER_AGENT'],"MSIE");
    $Firefox = strpos($_SERVER['HTTP_USER_AGENT'],"Firefox");
    $Mozilla = strpos($_SERVER['HTTP_USER_AGENT'],"Mozilla");
    $Chrome = strpos($_SERVER['HTTP_USER_AGENT'],"Chrome");
    $Chromium = strpos($_SERVER['HTTP_USER_AGENT'],"Chromium");
    $Safari = strpos($_SERVER['HTTP_USER_AGENT'],"Safari");
    $Opera = strpos($_SERVER['HTTP_USER_AGENT'],"Opera");

    if ($MSIE == true) { $navegador = "IE"; }
    else if ($Firefox == true) { $navegador = "Firefox"; }
    else if ($Mozilla == true) { $navegador = "Firefox"; }
    else if ($Chrome == true) { $navegador = "Chrome"; }
    else if ($Chromium == true) { $navegador = "Chromium"; }
    else if ($Safari == true) { $navegador = "Safari"; }
    else if ($Opera == true) { $navegador = "Opera"; }
    else { $navegador = $_SERVER['HTTP_USER_AGENT']; }

    return $navegador;
}
    
05.04.2016 / 20:28
6

Hello friend, see an example:

<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>

See working at this link: Example

    
15.08.2014 / 19:00
2

Adjust the code of the response SO-En :

if(preg_match('/(?i)msie [1-10]/',$_SERVER['HTTP_USER_AGENT']))
{
    // aqui é igual ou menor que 10
}
else
{
    // aqui maior que 10
}

Reference Question: SO-EN-PHP: If internet explorer 6, 7, 8, or 9

    
15.08.2014 / 18:00
2

You can use $_SERVER['HTTP_USER_AGENT']; and create a check function:

    function navegador($user_agent){
    if(preg_match('/rv:9.0/i',$user_agent)) {
    return "ok";
    }
}

With this done, just apply on the webpage:

$verifica = $_SERVER['HTTP_USER_AGENT'];
$executa = navegador($verifica);
$ok = "ok";
if($ok == $executa){
//Código para o aviso do navegador IE9.0
} else {
//demais navegadores 
}

Note that you can modify preg_match to identify any browser.

    
16.08.2014 / 10:32
1

Old, it is interesting that we know how to identify the browser and of course inform the user, but the most important thing is to adapt the system so that it works in ALL possible browsers ...

Unfortunately we can not predict which machine the user will use to access the system, here in the case of the company that works, most of our target audience has access to Firefox 28 down and IE8. Now that Chrome is starting to come in ...

Recently we had a headache to adapt so that everything worked in all browsers .....

Search for Responsive Web Design and start reading articles about it.

A tactic not very easy to implement, I have developed some time, use CSS to beautify the page according to the browser, of course it is not so complete, there are some errors, even more in Opera and Safari that use the same USER AGENT for Chrome:

#testehack{
    /* Cor preta para todos os navegadores */
    /*color:#000;*/
    /* Cor rosa para o IE7 */ 
    *background-image:url("imgs/ie7.jpg");
    *height: 330px;
    *width: 590px;
    /* Cor preta para o IE6 */
    _background-image:url("imgs/ie6.jpg");
}

/ * Hack for Internet Explorer 8 * /

#testehack{
      background-image:url("imgs/ie8.png")
@media all and (min-width:0) {
    #testehack{
        background:url("imgs/ie9.jpg");
        height: 310px
@-moz-document url-prefix(){
      #testehack{
            background-image:url("imgs/fix.png");
            height: 200px;
            width: 200px;
      }
}
/; width: 319px
@media screen and (-webkit-min-device-pixel-ratio:0){
      #testehack{
            background-image:url("imgs/chr-saf.jpg");
            height: 194px;
            width: 259px;
      }
}
/; } }
/; height: 290px
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0){
    #testehack{
        background-image:url("imgs/ope.png");
        height: 200px;
        width: 200px;       
    }
}
/; width: 284px
#testehack{
    /* Cor preta para todos os navegadores */
    /*color:#000;*/
    /* Cor rosa para o IE7 */ 
    *background-image:url("imgs/ie7.jpg");
    *height: 330px;
    *width: 590px;
    /* Cor preta para o IE6 */
    _background-image:url("imgs/ie6.jpg");
}
/; }

/ * Hack for Internet Explorer 9 * /

#testehack{
      background-image:url("imgs/ie8.png")
@media all and (min-width:0) {
    #testehack{
        background:url("imgs/ie9.jpg");
        height: 310px
@-moz-document url-prefix(){
      #testehack{
            background-image:url("imgs/fix.png");
            height: 200px;
            width: 200px;
      }
}
/; width: 319px
@media screen and (-webkit-min-device-pixel-ratio:0){
      #testehack{
            background-image:url("imgs/chr-saf.jpg");
            height: 194px;
            width: 259px;
      }
}
/; } }
/; height: 290px
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0){
    #testehack{
        background-image:url("imgs/ope.png");
        height: 200px;
        width: 200px;       
    }
}
/; width: 284px%pre%/; }

/ * Hack for Firefox * /

%pre%

/ * Hack for Chrome and Safari * /

%pre%

/ * Hack for Opera * /

%pre%     
15.08.2014 / 18:22
0

I think using this iframe can help.

<iframe src="http://www.whatismybrowser.com/feature/iframe?capabilities=true"width="600" height="370" style="border: none; overflow: visible !important; top:50%; left:50%; margin-left:-300px; margin-top:-185px; position:absolute;" id="wimb-detection-iframe"></iframe>

It returns, if browser cookies are enabled, if javascript is enabled, which version of flash, if it has java installed. The browser and version of it and which operating system.

    
15.08.2014 / 19:30
0

Fixed:

function Navegador() {
    $MSIE = strpos($_SERVER['HTTP_USER_AGENT'],"MSIE");
    $MSIE2 = strpos($_SERVER['HTTP_USER_AGENT'],"Trident");
    $Firefox = strpos($_SERVER['HTTP_USER_AGENT'],"Firefox");
    $Mozilla = strpos($_SERVER['HTTP_USER_AGENT'],"Mozilla");
    $Chrome = strpos($_SERVER['HTTP_USER_AGENT'],"Chrome");
    $Chromium = strpos($_SERVER['HTTP_USER_AGENT'],"Chromium");
    $Safari = strpos($_SERVER['HTTP_USER_AGENT'],"Safari");
    $Opera = strpos($_SERVER['HTTP_USER_AGENT'],"Opera");

    if ($MSIE == true) { $navegador = "IE"; }
    if ($MSIE2 == true) { $navegador = "IE"; }
    else if ($Firefox == true) { $navegador = "Firefox"; }
    else if ($Mozilla == true) { $navegador = "Firefox"; }
    else if ($Chrome == true) { $navegador = "Chrome"; }
    else if ($Chromium == true) { $navegador = "Chromium"; }
    else if ($Safari == true) { $navegador = "Safari"; }
    else if ($Opera == true) { $navegador = "Opera"; }
    else { $navegador = $_SERVER['HTTP_USER_AGENT']; }

    return $navegador;
}
    
20.07.2018 / 20:27