Is it good practice to detect browsers like this..?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    Is it good practice to detect browsers like this..?

    We all know that browser compatibility is almost always an issue. What I'd like to know is if this way of telling which browser the user is using is a good practice.


    Code:
    <?php
    $who = strtolower($_SERVER['HTTP_USER_AGENT']);
    
    if(preg_match("/msie/", $who)) { 
    	//code for IE 
    }
    elseif(preg_match("/mozilla/", $who)) { 
           //code for Firefox
    }
    else { 
    	//code for Other Browsers
    }
    ?>
    Thank you in advance.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    try this:





    Dan

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      It's interesting but if the user doesn't have javascript ..:(
      So you don't recommend using the php code above?

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        If it works, it works. I suggest you do some thorough testing though. But yeah, it looks OK to me. Probably even worth looking at the jQuery Browser code dlite922 posted to see how they do it and if you can improve yours at all.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          as mentioned in dlite992’s link, you should prefer feature detection over browser detection. the user agent string can be manipulated easily, after all.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            I haven't really thought about this too much, but why would anyone want to manipulate their agent string? If all it does is choose what content is served, there is not really a security risk is there? But, I think that feature detection is simpler and a better choice. Do you know how it handles IE "having" some features, but not really "having" them?

            Comment

            • zorgi
              Recognized Expert Contributor
              • Mar 2008
              • 431

              #7
              I like to use conditional comments for detection of IE browsers.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                You might manipulate your user-agent header to gain access to areas that only permit certain user-agents to view content.

                By the way, using preg_match to match simple strings is wasteful. Use strpos instead.

                Comment

                • londres9b
                  New Member
                  • Apr 2010
                  • 106

                  #9
                  Thank you all

                  Comment

                  • pradeepkr13
                    New Member
                    • Aug 2010
                    • 43

                    #10
                    use php function get_broswer();

                    But it's not advisable and not a good practice to write browser dependable code.

                    Comment

                    Working...