Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9

Thread: Check Browser

  1. #1
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Can I get the type of browser the user is using with PHP?

    So I can do something like the following:

    Code:
    if($browser == "IE") {
    print "I'm very dissapointed.";
    }
    I know how to do this using Javascript, but I don't really like Javascript for something like this, I use it more for dynamic pages.

    Thanks
    tekp :cheeky: tekponline.com

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Yes, the built in superglobal is $_SERVER['HTTP_USER_AGENT']; .

    http://www.psychedelix.com/agents.html is a list of the user agents..

    I wrote a script that keeps track of user visits.. including the user agent, output can be found at http://bfsog.co.uk/logme.htm

    Hope this helps

  4. #3
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Most experts say NOT to sniff browsers for anything important. Instead, it can be better to test capabilities (but this advice applies more to JavaScript).

    Also of note: Most non-IE browsers (incl. Firefox, Opera) can be set to "lie" and say they are IE or another browser!
    Because of this, it may be smarter to use a library already out there for browser sniffing than trying to write one yourself. (Opera, for example, says something like "Oh yeah I'm IE... blah... but I'm Opera." Your script would probably ID this as IE, but *some* other libraries correctly ID it as Opera.

    Finger cramp.
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  5. #4
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Ok..

    So those values of HTTP_USER_AGENT look pretty complex, would I have to run an if($browser == "") on the whole string? Which is the one for an IE browser?
    tekp :cheeky: tekponline.com

  6. #5
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    You could use ereg() I think IE includes MSIE in it..

    * IE 6: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) so yes.

    PHP Code:
    <?php
    $browser 
    $_SERVER['HTTP_USER_AGENT'];
    if(
    ereg("MSIE"$browser))
       {
         echo 
    "I am shocked and dismayed at your browsing choice!";
       }
    ?>
    Hope this helps.

  7. #6
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Yaay thanks,

    I'll try that ^_^
    tekp :cheeky: tekponline.com

  8. #7
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    ereg(), or a strstr()/stristr()... You're not actually pattern-matching, just looking for a single substring.

    Either way. I would just expect strstr to be fastest. (Either way, Opera ID'ing itself as IE would come thru as IE, which may or may not be what you want...)
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  9. #8
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    strstr will return the position of the string, or false if not found, so if Opera does do as you say, both are open to problems.

  10. #9
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Agreed

    Quote Originally Posted by bfsog
    strstr will return the position of the string, or false if not found, so if Opera does do as you say, both are open to problems.
    Quote Originally Posted by straight_up
    (Either way, Opera ID'ing itself as IE would come thru as IE, which may or may not be what you want...)
    emphasis mine. We are in agreement.
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:25 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com