SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check browser and platform with PHP?

    Hi,

    I'm hoping to check if the browser is Safari and the platform is Windows using a PHP if statement.

    How do I achieve this?


    Thanks for any helpers.

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats rare combination
    For such a single task you can directly parse User-Agent header, which comes into
    $_SERVER['HTTP_USER_AGENT'] variable.

    For more complicated parsing take a look on get_browser() function.

  3. #3
    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)
    Good advice.

    A quick Google for user-agent string provided this Michael.

    PHP Code:
    <?php
    if(false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') && false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Safari'))
    {
        
    #a pretty good chance (but not 100%) this is Safari on Windows.
    }
    ?>
    Out of curiosity, why do you need to do this?
    @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.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant, thanks for the links guys.

    I've tried running a get_browser function but to no avail.

    I think I just need to store the user agent in a variable and then (somehow) search for both safari and windows in that string. It's that somehow bit that's going to win me the car

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Long story. I used litmus and browsercam to check a whole stack of sites I've built and one form on one site has a Safari/Win issue that wasn't there before.

    Literally using it just to add a <br /> tag. Is nice!

    strpos - that's what I was thinking. Thanks again Anthony.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Just remember that the USERagent is user configurable and can contain anything your visitor wants. What are you going to do if their useragent says they are running IESafariFirefoxOperaChrome on windowslinuxos/x?
    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="^$">

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good call felgall. TBH I'm not too fussed if a user goes to the bother of doing that, it'll only have a slight design impact

    What would be the other way, except for UserAgent? Is there one?

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool.

    So worst thing that happens is it looks a little off, for someone who dabbles with their UserAgent. They'll get a kick. Everybody's happy!


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
  •