User agent - extracting the browser type

Discussion in 'PHP' started by Weirfire, Mar 13, 2006.

  1. #1
    I've got this variable from HTTP_USER_AGENT

    Which comes up as Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1

    Does anyone know how I can determine which browser that is actually looking at the site. How could I determine between IE and Mozilla Firefox?
     
    Weirfire, Mar 13, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A simple way that I use is:
    $useragent = getenv("HTTP_USER_AGENT"); 
    if (preg_match("/MSIE/i", "$useragent")) 
    {
    $browser= "MSIE";
    }
    else if (preg_match("/Mozilla/i", "$useragent")) 
    {
    $browser= "Firefox";
    }
    else{
    ...........
    }
    PHP:
     
    mad4, Mar 13, 2006 IP
    Weirfire likes this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    I had 1 come up that had Mozilla and MSIE in the same user agent.

    Will that make a difference. I don't really know much about how the user agent variable is configured.

    Thanks for the help Mad :)
     
    Weirfire, Mar 13, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try http://www.phpclasses.org/browse/package/1663.html

     
    T0PS3O, Mar 13, 2006 IP
  5. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #5
    An error in the script I posted is that you should detect the word Firefox rather than Mozilla to find the firefox browser.:rolleyes:

    This ties up with how the script at phpclasses does it.
     
    mad4, Mar 13, 2006 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Thanks T0Psy baby. I'll have a read this afternoon.
     
    Weirfire, Mar 13, 2006 IP