1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to Detect browser to load different templates

Discussion in 'PHP' started by JEET, Feb 10, 2014.

  1. #1
    Hi,
    I'm trying to load a different template for different devices like phones, ipads, pc etc.
    I know I can get USER_AGENT of $_SERVER to find out a device type.
    The problem is to maintain a long list of USER_AGENT types.

    How do I do this without keeping a long list of USER_AGENT types?

    The templates are all fluid designs (no fixed pixel sizes).
    So basically all I need to know is if it's a wide screen PC, or a small screen phone, or an ipad (whatever size they come in), and then load appropriate template to match the width.

    Any code is appreciated.
    Thanks
     
    JEET, Feb 10, 2014 IP
  2. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #2
    you can try using jquery?

    <script>
      if ($(window).width() > 1024) {
         //todo: do something here
    }
    else {
       //todo: load template for width of 1024, or whatever
    }
    </script>
    PHP:
     
    wren11, Feb 10, 2014 IP
  3. Iconiplex

    Iconiplex Member

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    43
    #3
    You don't use PHP for this. You use CSS media queries to create responsive themes. Plenty of tutorials on Google for this, including the resolutions you need to match.
     
    Iconiplex, Feb 10, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Or just learn how to do it properly, without hacks, via CSS @media? Read up.
    There are several ways to do this, but none are reliable, and every time (which is quite often) there's a new browser out, you need to update the code. Hence, use the CSS approach
     
    PoPSiCLe, Feb 10, 2014 IP
    deathshadow and sarahk like this.
  5. HolyRoller

    HolyRoller Well-Known Member

    Messages:
    552
    Likes Received:
    27
    Best Answers:
    1
    Trophy Points:
    150
    #5
    HolyRoller, Feb 11, 2014 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    ThePHPMaster, Feb 11, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    I do not recommend using browscap, simply because, as I stated above, it's not possible to direct-link to it, you have to run it locally (or at least host it somewhere yourself), and that means that every time there's a new browser-version out, which the current script doesn't cover, it has to be updated (given that the script itself is updated, which happens sporadically). Depends a little bit on how specific you want to be as well, of course.
     
    PoPSiCLe, Feb 12, 2014 IP
    deathshadow likes this.
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    The proper term for this is "browser sniffing" -- and so far folks are right... Don't... JUST DON'T.

    If you have to dick around server side sniffing out which browser is being used, there is probably something horrifically and disastrously wrong with both your HTML and CSS.

    You should have ONE markup; that should be semantic with complete separation of presentation from content. You then use the MEDIA attribute to target specific device types, and media queries to adjust the layouts to the available space. One markup, many layoutS... no server or client side scripting involved.

    ... and all of said layoutS being elastic and semi-fluid.
     
    deathshadow, Feb 12, 2014 IP
  9. internationalhost

    internationalhost Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    If you would like to do it in PHP, not recommended. Fine the browser using PHP code and then do an if, elseif, else(other browsers) statement.
     
    internationalhost, Feb 12, 2014 IP