Page 1 of 1

How do you identify a user as using an iPhone?

Posted: Sat Jul 30, 2011 1:33 pm
by simonmlewis
Hi
We want to redirect users to a smaller browser version of a web site for iPhone users.

How do you identify the users device as an iPhone??

Hope someone can help.

Re: How do you identify a user as using an iPhone?

Posted: Sat Jul 30, 2011 11:39 pm
by xtiano77
You can try using the "$_SERVER['HTTP_USER_AGENT']" together with the preg_match( ) to see if the word "iPhone" is found in the return string which describes the browser. I tried it on my iPhone and it worked just fine.

Code: Select all

preg_match("/iPhone/", $_SERVER["HTTP_USER_AGENT"]);
Check out the manual at: http://www.php.net/manual/en/reserved.v ... server.php
This is also possible via the JavaScript "navigator" object: http://www.w3schools.com/js/js_browser.asp

Hope this helps.

Re: How do you identify a user as using an iPhone?

Posted: Mon Aug 01, 2011 4:49 am
by simonmlewis
Thanks - it does with FF's user agent. Just waiting to hear from a colleague with an iPhone.
Can the same be done for Blackberry and Android?

Re: How do you identify a user as using an iPhone?

Posted: Tue Aug 02, 2011 9:38 pm
by xtiano77
I believe you can by using the $_SERVER["HTTP_USER_AGENT"]. Check out the link[s] below:

http://php.net/manual/en/function.get-browser.php

Re: How do you identify a user as using an iPhone?

Posted: Wed Aug 03, 2011 8:11 am
by simonmlewis
Great I found it.
Also, if you have a friend who has one of those phones, I found it how easy it is to find the name you need.

My next question is how to question the AGENT twice in the same script:

Code: Select all

if (preg_match("/iPhone/", $_SERVER["HTTP_USER_AGENT"]))
I want to add if it is a "BlackBerry" too now. And then perhaps "Android". I'm sure you can just add them all in that preg_match, but how?

Re: How do you identify a user as using an iPhone?

Posted: Wed Aug 03, 2011 6:52 pm
by xtiano77
You should be able to use the array returned from the "get_browser( )" function and retrieve the "browser" value together with a "switch( ){ }" statement. Check out the link below to see what I mean:

http://www.tuxradar.com/practicalphp/16/5/0

Cheers and good luck to you.

Re: How do you identify a user as using an iPhone?

Posted: Thu Aug 04, 2011 2:31 am
by simonmlewis
No I don't need any of that - I just need to know how you say "is it iphone, or Android, or Blackberry... in that Preg Match query.

Re: How do you identify a user as using an iPhone?

Posted: Thu Aug 04, 2011 3:01 am
by Dodon
try something like:

Code: Select all

if (preg_match("/(iPhone|Blackberry|Android)/i", $_SERVER["HTTP_USER_AGENT"]))
 

Re: How do you identify a user as using an iPhone?

Posted: Thu Aug 04, 2011 3:46 am
by simonmlewis
Perfect. I don't know if it is 'Android', but I know it is "BlackBerry" and it works great.

Thanks.