5

I found this code to detect the browser via php:

<?php
$msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false;
$firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
$safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
$chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
?>

<?php
//Firefox
if ($firefox) {
echo 'you are using Firefox!';
echo '<br />';
}

// Safari or Chrome. Both use the same engine - webkit
if ($safari || $chrome) { 
echo 'you are using a webkit powered browser';
echo '<br />';
}

// IE
if ($msie) {
echo '<br>you are using Internet Explorer<br>';
echo '<br />';
}?>

Source

But the code does not include the possible versions of IE. Did something like this:

// IE7
if ($msie7) {
echo '<br>you are using Internet Explorer 7<br>';
echo '<br />';
}

Could someone help me with this? Wanted to improve the code including support IE ​​versions.

Community
  • 1
  • 1
Joselito
  • 63
  • 1
  • 1
  • 3
  • Print this var $_SERVER["HTTP_USER_AGENT"] from diferents IE browsers (versions) and see how you can parse it to show diferents messages – Tommy Feb 18 '14 at 16:16
  • Warning: `HTTP_USER_AGENT` can be manipulated by the client requesting the web page. So don't relay on it fully. – bansi Feb 18 '14 at 16:20
  • http://il1.php.net/get_browser - check the comments too. – Amnon Feb 18 '14 at 16:21
  • you may be interested in these 2 classes http://www.phpclasses.org/package/6369-PHP-Detect-the-type-of-browser-accessing-the-site.html and http://ellislab.com/codeigniter/user-guide/libraries/user_agent.html – bansi Feb 18 '14 at 16:23

3 Answers3

6

Try this:

<?php
    $ie6 = (ereg("MSIE 6", $_SERVER["HTTP_USER_AGENT"])) ? true : false;
    $ie7 = (ereg("MSIE 7", $_SERVER["HTTP_USER_AGENT"])) ? true : false;
    $ie8 = (ereg("MSIE 8", $_SERVER["HTTP_USER_AGENT"])) ? true : false;

    if ($ie6 || $ie7 || $ie8) {
        // Do fallback stuff that old browsers can do here
        echo "You are using IE";
    } else {
        // Do stuff that real browsers can handle here
    }
?>
vqdave
  • 2,361
  • 1
  • 18
  • 36
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
  • 11
    I like it when you say `real browsers` for browsers other than IE – asprin Feb 18 '14 at 16:29
  • 4
    Sidenote: [`ereg()`](http://www.php.net/ereg) This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. [`preg_match()`](http://www.php.net/preg_match) is the suggested alternative to this function. – Funk Forty Niner Feb 18 '14 at 16:35
  • 1
    `ereg` is deprecated, but in this case `preg_match` isn't a good alternative. Use something like `strpos`, which is much quicker than the regex engine. Never use regex if you don't need it. This would be better: `$ie_user = !!preg_match('/MSIE (6|7|8)/', $_SERVER['HTTP_USER_AGENT']);`. – Justin Oct 21 '15 at 16:29
1

This PHP function works perfectly. But I do not know how to make PHP discriminate between the different versions of "MSIE"

    <?php 
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ){
        header( 'Location: http://www.domain.com' ) ;
    }
?>

Very Thanks

Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56
-1

There is no reliable way for PHP to know, nor care, which browser is calling upon it. By design, server-side languages have no reason to consider what browser you are using before executing code. $_SERVER['HTTP_USER_AGENT'] is a shim at best and changes so frequently that you are bound to have it screw you over sooner rather than later.

Your only goal as a web developer should be to write both CSS and JS code which is either cross-browser compatible or use a conditional <!--[if IE 8]><![endif]--> shim which has apparently been dropped by IE11 anyways...

In JS you need to check if a function exists before calling it or figure out if the JS engine which supports it is available within your browser target scope.

With that being said, yes there are libraries out there which attempt to decipher $_SERVER['HTTP_USER_AGENT'] but the information source is flawed in the first place so the library is useless from the moment you try using it.

Here is some cross-browser CSS:

.someClass{
    -webkit-box-shadow: 0 0 5px black;
    -moz-box-shadow:    0 0 5px black;
    box-shadow:         0 0 5px black;

    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);

    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;

    /* IE supports nothing above? Too bad, it will ignore it */
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77