Check user browser in PHP

Asked

Viewed 6,579 times

9

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

  • here in the English version you have your answer http://stackoverflow.com/questions/5302302/php-if-internet-explorer-6-7-8-or-9

  • +1 for "that" =)

  • Just be sure to consider the sheer amount of Windows XP on the market yet.

8 answers

6

Hello friend see an example:

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

see working on this link : Example

2

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

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

With this done, just apply on the web page:

$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 the preg_match to identify any browser.

2

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, see here in the case of the company I work, 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.....

Web Responsive Design Research and starts reading articles about it.

A tactic not very easy to be implemented, I developed some time, use CSS to beautify the page according to the browser, of course is not so complete, there are some errors, even more in Opera and Safari that use the same USER AGENT 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")\0/;
      height: 290px\0/;
      width: 284px\0/;
}

/* Hack for Internet Explorer 9 */

@media all and (min-width:0) {
    #testehack{
        background:url("imgs/ie9.jpg");
        height: 310px\0/;
        width: 319px\0/;
    }
}

/* Hack for Firefox */

@-moz-document url-prefix(){
      #testehack{
            background-image:url("imgs/fix.png");
            height: 200px;
            width: 200px;
      }
}

/* Hack for Chrome and Safari */

@media screen and (-webkit-min-device-pixel-ratio:0){
      #testehack{
            background-image:url("imgs/chr-saf.jpg");
            height: 194px;
            width: 259px;
      }
}

/* Hack for Opera */

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

0

There’s another way to do it

<!DOCTYPE html>
<html>
<head>
  <title>Verifica CHROME</title>

  <!-- Inserir este SCRIPT no inicio da pagina ou no header -->
  <script type="text/javascript">
    var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
    if (!isChrome) 
    { 
      window.location.href = "PAGINA_COM_MENSAGEM_DE_ERRO_POR_NAO_SER_CHROME.html";
    } 

    document.body.innerHTML = output;
  </script>
  <!-- fim do script -->

</head>

<body>
Você está no chrome então OK pode seguir
#Código vai aqui! 
</body>
</html>

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

0

I think the use of this iframe can help you.

<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 active, if javascript is enabled, which flash version, if java is installed. The browser and version thereof and which operating system.

0

Corrected:

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

  • Why would MSIE2?

Browser other questions tagged

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