SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Browser Detection

    Hello,

    I'm putting a site together and I need to deliver an extra bit of CSS if the user is using anything but IE. I was wondering whether it's possible to determine whether the user is using IE (maybe using the HTTP_USER_AGENT variable) and echo out a little bit of CSS?

    If this can't be done with PHP, can anyone point me in the right direction of a Javascript solution?

    Cheers,

    Jon

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi Jon,

    Try reading this thread, it may answer some questions you have.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good solution would be to use conditional comments, although it's got nothing to do with PHP.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replies.

    I think I've sorted it out by using the code below:

    $browser = $_SERVER['HTTP_USER_AGENT'];
    if (!strpos($browser,"MSIE")){
    echo '..................';
    }

    It wasn't for anything critical, just trying to get CSS border colours to look the same when I was using "groove", there was a huge difference and my client didn't like it.

    I expect that the above code would be too much of a blunt instrument for most circumstances.

    Cheers,

    Jon

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by jonpugh114 View Post
    $browser = $_SERVER['HTTP_USER_AGENT'];
    if (!strpos($browser,"MSIE")){
    echo '..................';
    }
    I could easily have all the browsers on my computer set up so that the only one that doesn't contain MSIE in the useragent field is Internet Explorer - if I did that then your code would break in all browsers. Don't rely on a free format user controlled value that someone might change. It you need to distinguish between IE and other browsers use Microsoft conditional comments either in the HTML or JScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed. Set your CSS to what it should be in all the browsers and use conditional comments to specify IE-specific CSS.

    Kinda reverse of what you're trying to do really...

  7. #7
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Here you go :

    HTML Code:
    <!--[if lte IE 6]>
    <link href="/path_to_css/css_for_ie6.css" rel="stylesheet" type="text/css" />
    <![endif]-->
    This same fashion you can change for other browsers like IE7, FF etc. CSS automatically decide which one to use as per browser type.

    PHP's USER_AGENT can always be overridden. So using conditional statements are better idea than USER_AGENT.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by viveknarula View Post
    This same fashion you can change for other browsers like IE7, FF etc. CSS automatically decide which one to use as per browser type.
    No - those conditional comments only work in Microsoft programs so you can test for various versions of Internet Explorer (IE), Microsoft Office (MSO) etc but not for software from other providers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    but not for software from other providers.
    What do you mean by other providers ? Firefox, Safari.... ?

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simshaun View Post
    Agreed. Set your CSS to what it should be in all the browsers and use conditional comments to specify IE-specific CSS.

    Kinda reverse of what you're trying to do really...

    I didn't know about this, I'm sure that would work.

    Cheers,

    Jon

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by viveknarula View Post
    What do you mean by other providers ? Firefox, Safari.... ?
    I mean any software not from Microsoft - that includes all browsers except for Internet Explorer.

    The Microsoft conditional comments only work for Microsoft programs such as Internet Explorer and Microsoft Office. Its original intended use was to allow files to be exported from one program into "HTML" format and then imported into a different Microsoft program where the conditional comments would allow the correct formatting to be reapplied using the system used by that program. Its main use prior to people realising that it could be used to apply patches to web pages was to take an Office document from a recent version of Office and convert it back to an earlier version.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •