What is the best way to detect browser with php?

Can someone tell me the best way to detect browser using php? IE 6 and 7 are terrible when it comes to full browser compatibility with CSS, so my site is not going to support a version of IE that is older than 8.

I use $_SERVER['HTTP_USER_AGENT']to detect the browser, but I was told that this is not a good way to do this, as browsers can lie and send any user agent information that they want. So, does anyone know of a reliable way to detect the type of browser the client is using?

+3
source share
5 answers

You cannot reliably determine what a browser is. It is so simple.

.

- , PHP- .

Javascript , , PHP .

- IE6, . IE6 2,5%. CSS. IE7 - , 1,6 , IE6, , .

, , .

, CSS IE. :

, , JQuery.

+4

PHP - PHP.

, IE lt 8 CSS ( , - ), , , HTML:

<head>
...
<!--[if IE lt 8]>
  <link rel="stylesheet" type="text/css" href="ie-styles.css" />
<![endif]-->
</head>
<body>
<!--[if IE lt 8]>
  <div id="ie-only">
    <p>Please get a <a href="http://www.abetterbrowser.org/">better browser</a>.</p>
  </div>
<![endif]-->
...
</body>

, , , : , IE, -, , IE HTML.

, IE wobsite, , , crummy .

cookie PHP:

<!--[if IE lt 8]>
<script type="text/javascript">document.cookie='isie=oh-crud-you-use-ie';</script>
<![endif]-->
+1

( .)

IE6 7, :

<form action="wherever.php" method="post">
    <!--[if lt IE 8]>
    <input type="hidden" name="too_old" value="1" />
    <![endif]-->
</form>
<script>
document.getElementsByTagName('form')[0].submit();
</script>

, . ( , JS, .)

, , PHP:

$location = empty($_GET['too_old']) ? 'good_page.wherever' : 'bad_page.whatever';
header('Location: '.$location);

inb4 " ".

0

The best way to detect a browser is to use the php class below. I created this for my site a year ago. It works with chrome, firefox, opera and internet explorer.

 <?php 
    class Browser { 
        public static function detect() { 
            $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); 
            if ((substr($_SERVER['HTTP_USER_AGENT'],0,6)=="Opera/") || (strpos($userAgent,'opera')) != false ){ 
                $name = 'opera';
            } 
            elseif ((strpos($userAgent,'chrome')) != false) { 
                $name = 'chrome'; 
            } 
            elseif ((strpos($userAgent,'safari')) != false && (strpos($userAgent,'chrome')) == false && (strpos($userAgent,'chrome')) == false){ 
                $name = 'safari'; 
            } 
            elseif (preg_match('/msie/', $userAgent)) { 
                $name = 'msie'; 
            } 
            elseif ((strpos($userAgent,'firefox')) != false) { 
                $name = 'firefox'; 
            } 
            else { 
                $name = 'unrecognized'; 
            } 
            if (preg_match('/.+(?:me|ox|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name']=='safari' ) { 
                $version = $matches[1]; 
            }
            if (preg_match('/.+(?:me|ox|it|on|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name']!='safari' ) { 
                $version = $matches[1]; 
            }
            else { 
                $version = 'unknown'; 
            } 

            return array( 
                'name'      => $name, 
                'version'   => $version,
            ); 
        } 
    } 
    $browser = Browser::detect(); 
    echo 'You browser is '.$browser['name'].' version '.$browser['version']; 
    echo "<br />";
    ?> 
0
source

You can only reliably detect the browser user agent using Javascript. For PHP, you must rely on the contents of $ _SERVER ['HTTP_USER_AGENT'].

-1
source

Source: https://habr.com/ru/post/1788249/


All Articles