SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Browser Sniffing

    Hey

    I have the following code which I've just started using on my site;

    PHP Code:
    if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { echo 'go and upgrade'; die(); } 
    This detects Internet Explorer and shows the message, otherwise it shows the website. However I've just tested in Opera and it displays this message too when it should be showing the site.

    I'm far from a PHP guru so I don't know what to change to only block out IE.

    Any help?


    Chris

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hey Chris,

    I think that because msie shows first in the string returned you need to order your conditional accordingly.

    This is Opera 7
    mozilla/4.0 (compatible; msie 6.0; windows nt 5.1) opera 7.51 [en]

    Here's a conditional that'll work for you. You could change it to a switch statement to make it a bit cleaner:
    PHP Code:
    <?php  
    $client 
    strtolower($_SERVER['HTTP_USER_AGENT']);
    if(
    strpos($client'opera')) { echo 'It\'s Opera. You get the point'; die(); }
    if(
    strpos($client'msie')) { echo 'It\'s IE. Go and upgrade'; die(); }
    if(
    strpos($client'webtv')) { echo 'It\'s WebTV. Who would have thought?'; die(); }
    if(
    strpos($client'gecko')) { echo 'It\'s Gecko. Good on ya!'; die(); }
    if(
    strpos($client'safari')) { echo 'It\'s Safari. Must be OSX'; die(); }
    if(
    strpos($client'konqueror')) { echo 'It\'s Konqueror. Odds are you\'re running Linux'; die(); }
    ?>
    Cheers - Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Cyberia
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') && !strpos($_SERVER['HTTP_USER_AGENT'], 'Opera')) { echo 'go and upgrade'; die(); } 

  4. #4
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haven't tried these out but by the looks of things they'd just show the message go and upgrade or whatever for each browser.

    I want to only block out MSIE and allow every other browser to view the site....

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by a-drive
    I want to only block out MSIE and allow every other browser to view the site....
    Wow... Like a reverse MSN thing

    Ok really simple to do. You know how conditionals work right?

    Well your code looks like it anyway. Take the one that mikis put up and then redirect the browser of the IE user away from your page to a page that tels them to upgrade. In my code sample we'll call it upgrade.php. Everyone else gets to vew the page.
    PHP Code:
    $client strtolower($_SERVER['HTTP_USER_AGENT']); 
    if(
    strpos($client'msie') && !strpos($client'opera')) 

        
    //THIS IS AN EVIL MSIE BROWSER REDIRECT THEM
        
    header("Location:upgrade.php"); 

    How's that?
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  6. #6
    SitePoint Zealot Koobi's Avatar
    Join Date
    Nov 2003
    Location
    Sri Lanka
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have the browscap.ini file, then you should consider using get_browser()

  7. #7
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awasson thats dude, that works great!

    I just got tired of having to work on workarounds for IE's lack of CSS knowledge so decided to f*** them altogether.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Cyberia
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by a-drive
    awasson thats dude, that works great!
    Umm, just that you know, it is almost exactly the same as what I wrote, and you didn't want to try

  9. #9
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by a-drive
    awasson thats dude, that works great!

    I just got tired of having to work on workarounds for IE's lack of CSS knowledge so decided to f*** them altogether.
    Yeah, as mikis said, that's the same as his code - always best to try and give credit to the rigth person.

    Also, cutting out 90% of the population generally isn't a great idea - unless it's like, an admin panel or something internal/specific.

  10. #10
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikis
    Umm, just that you know, it is almost exactly the same as what I wrote, and you didn't want to try
    Sorry mikis

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikis
    Umm, just that you know, it is almost exactly the same as what I wrote, and you didn't want to try
    That's why I gave you credit mikis:
    Take the one that mikis put up and then redirect the browser of the IE user away from your page to a page that tels them to upgrade.
    Off Topic:


    Then I went on a feverish rant about how your code was no different than mine except that your was contained on one line with the AND conjunction.... Bla bla bla.... Until it occured to me that the reason I used your code was for exactly that reason. oops

    I'm going to blame my sensless rant on the flu.

    And, yes mikis' is the creditee
    PS:
    Don't forget to convert to ether upper or lower case with these types of operations. It'll save you lots of troubleshooting time.


    And no worries A-Drive. I agree about the CSS troubles in IE. Nothing but workarounds. See ya,
    A
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  12. #12
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Cyberia
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, np It's not about credits, it is that people should try to *understand* answers to their questions, not just copy & paste code until it works.

    And I agree: it is very rude do deny access to majority of internet users. I would rather use that code to display them nice Get Firefox logo

  13. #13
    SitePoint Wizard silver trophy KLB's Avatar
    Join Date
    Nov 2003
    Location
    Maine USA
    Posts
    3,781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind with all of these browser detection routines, it is not uncommon for alternative browsers like Firefox, Opera, etc., as well as proxies and other web browser related apps to report themselves as Internet Explorer. It is counter intuitive, but the browser's user agent string is actually not a great way of detecting what browser is being used.

    I don't know fail safe tests for all browsers, but I do know that you can always detect Opera with the following JavaScript:
    Code:
     if (window.opera) {[your instructions]}
    Also you can feed IE stylesheets that no other browser will know to use with the following code:
    Code:
    		<!--[if IE 5]>
    			<link href="/IE5.css" rel="stylesheet" type="text/css">
    		<![endif]-->
    		<!--[if IE 6]>
    			<link href="/IE6.css" rel="stylesheet" type="text/css">
    		<![endif]-->
    Because non-MSIE browsers will see this code as a comment, they will ignore it and it will validate to W3C specifications, which is a very good thing.
    Ken Barbalace: EnvironmentalChemistry.com (Blog, Careers)
    InternetSAR.org
    Volunteers Assist Search and Rescue via Internet
    My Firefox Theme: Classic Compact
    Based onFirefox's default theme but uses much less window space

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikis
    Heh, np It's not about credits, it is that people should try to *understand* answers to their questions, not just copy & paste code until it works.
    That's for sure

    Have a good weekend,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  15. #15
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Ontario Canada
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with this is so many people clone their user agent to look like another browser, and for some reason, some people even make it look like IE. What's to be proud of by stating your using that crap?

    So if you do stuff based on browsers these people who spoof their user agent may run into problems.

    But they should not be screwing with their settings in first place. :P


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •