Detecting Safari with PHP

Hi,

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

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') && !strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')) {
	$browser = 'Safari';
}

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.

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.

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:

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:

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.

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.

http://green-beast.com/experiments/php_browser_sniffer.php

Thanks for the link, I bookmarked it. It will definitely help me from now on.

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.

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.

How do you turn it on? I don’t think changing user agents is something non-technical people does.

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.

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).