SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting Safari with PHP

    Hi,

    Is the following enough for detecting Safari? Or is there any other things I should be aware of?

    PHP Code:
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') && !strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')) {
        
    $browser 'Safari';


  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi nayen,

    I would say that browser sniffing like this is not a good idea.
    Check out felgall's post here, as to why.

    What is the reason you need to detect Safari?
    Couldn't you rely on feature detection, instead?

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi nayen,

    I would say that browser sniffing like this is not a good idea.
    Check out felgall's post here, as to why.

    What is the reason you need to detect Safari?
    Couldn't you rely on feature detection, instead?
    Whether it is a good idea or not, I need to check if the user is trying to view the page with Safari because one thing I have on that page doesn't work on Safari and it is not about a feature. There is no way to make it work, therefore I will show a slightly different page to Safari users.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Just out of interest, what is it that doesn't work?
    Is it something that Safari doesn't support?

    With your PHP code, are you hoping to catch desktop and mobile Safari, or just one or the other?

  5. #5
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Just out of interest, what is it that doesn't work?
    Is it something that Safari doesn't support?

    With your PHP code, are you hoping to catch desktop and mobile Safari, or just one or the other?
    You can see my previous question to see what the issue is. I have made a lot of research after that and came into conclusion that it is a Safari thing that is not curable on my side.

    I am hoping to catch desktop Safari because mobile user agent check is done on a previous step.

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Oh, ok. That looks like quite a gnarly problem. I think you were in the JavaScript forum with it recently, no?
    Anyway, the code you posted looks fine to me:

    Latest Safari UA:
    Code:
    Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
    Latest Chrome UA:
    Code:
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17
    Bearing in mind the above-mentioned caveats, this should work as expected.

    BTW, I don't know if you already found it, but when doing this kind of thing, this site can be quite helpful.

  7. #7
    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)
    The version of Safari I have uses any of the following useragents in addition to the ones that include Safari in the text:

    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
    Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11

    or you can select Other... and set it to anything at all.
    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="^$">

  8. #8
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)

  9. #9
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    BTW, I don't know if you already found it, but when doing this kind of thing, this site can be quite helpful.
    Thanks for the link, I bookmarked it. It will definitely help me from now on.

    Quote Originally Posted by felgall View Post
    The version of Safari I have uses any of the following useragents in addition to the ones that include Safari in the text:

    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
    Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11

    or you can select Other... and set it to anything at all.
    Can you please tell me where you select those in Safari? Do you think changing the useragent is something a regular visitor would do? I am aware of what you are trying to say but this is not used on a publicly available website, and the target users are not technical type of people who would mess with changing useragents of browsers. This is an assumption but as I start to get feedback, I will see ways for improving my script.

    Thanks for the link.

  10. #10
    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)
    Quote Originally Posted by nayen View Post
    Can you please tell me where you select those in Safari? Do you think changing the useragent is something a regular visitor would do?
    It is the second option down in the Develop menu - that menu iis not on by default so it has to be deliberately turned on to get it to work.

    It is something that is likely for visitors to do as there are man scripts using browser sensing and only recognising specific browsers - perhaps only Internet Explorer - and so people using other browsers must change their useragent to allow themselves access to sites where the site author has stupidly implemented browser sensing. Usually any problems such a test is intended to fix are long in the past and the code simply hasn't been updated to reflect that all browsers now will work with their site.
    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="^$">

  11. #11
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    It is the second option down in the Develop menu - that menu iis not on by default so it has to be deliberately turned on to get it to work.
    How do you turn it on? I don't think changing user agents is something non-technical people does.

    Quote Originally Posted by felgall View Post
    It is something that is likely for visitors to do as there are man scripts using browser sensing and only recognising specific browsers - perhaps only Internet Explorer - and so people using other browsers must change their useragent to allow themselves access to sites where the site author has stupidly implemented browser sensing. Usually any problems such a test is intended to fix are long in the past and the code simply hasn't been updated to reflect that all browsers now will work with their site.
    I get your points, I am also against sensing browser for things that could be cured on the webmaster side but my issue is not one of them and the page I am building is not a publicly available page, besides people who will view it will most probably have no idea what a user agent is.

  12. #12
    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)
    Quote Originally Posted by nayen View Post
    the page I am building is not a publicly available page
    In that case you should simply be able to dictate what browsers those who have access to the page can use to access it. There are lots of pages on the web that are restricted to specific groups of people that do not work in all the popular browsers (as well as some that try to tell you incorrectly that they don't work with the browser you are using).
    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
  •