0

I know some very light PHP and decided to start from the ground up and was looking at php.net http://php.net/manual/en/tutorial.useful.php On this page, it mentions in an example how to check for IE.

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
    echo 'You are using Internet Explorer.<br />';
}
?>

I have a very small test page made up that I'm going through the section here with and this is what I have written down...

<html>
    <head>
        <title>PHP startup testing page</title>
    </head>
    <body>
<?php 
        echo "<p>Hello World</p>";
        echo $_SERVER['HTTP_USER_AGENT']; //outputs the kind of browser the visitor is using.
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
            echo 'You are using Internet Explorer.<br />';
        }
?>
    </body>
</html>

What I don't understand is why when I load this into Internet Explorer, the output onto my screen is Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko and the if statement thus returns nothing. Is there something very basic I'm not getting?

JoeL
  • 710
  • 4
  • 18
  • For what it is worth the user agent string is provided to PHP and is not reliable. If you need something specific I believe client side feature detection is going to serve you better. – Crackertastic Oct 28 '15 at 20:21

1 Answers1

2

IE 11 no longer uses MSIE in the user agent string, it is also bad practice to detect for a browser you should detect for features with libraries like Modernizer https://modernizr.com/, here is a link on IE 11 user agent strings https://msdn.microsoft.com/en-us/library/ms537503%28v=vs.85%29.aspx

cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • Wow thanks a lot for that information! If you don't mind me asking then, what is the harm in searching the string for "Trident"? – JoeL Oct 28 '15 at 20:33
  • @JoeL take a look at this list of all the browsers that use `Trident` http://www.useragentstring.com/pages/Browserlist/, its much easier to make your site based on features of the browser then based on the browser, in addition users can change their User Agent String. – cmorrissey Oct 28 '15 at 20:46
  • point well taken. I will look into modernizr.com and how I can use that in my browser checks. This will be my last one and then I'll move on my merry way. Should I still be good with using php.net for beginner stuff? Or was this kind of an outlier in terms of outdated information. @cmorrissey – JoeL Oct 28 '15 at 20:52
  • 1
    php.net is great for reference, check out http://www.phptherightway.com/ for more information, and check your local book store for a book that was published in the past year that covers php, mysql, html, css, and js. – cmorrissey Oct 28 '15 at 20:55