How do you identify a user as using an iPhone?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you identify a user as using an iPhone?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

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

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

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

Post by Dodon »

try something like:

Code: Select all

if (preg_match("/(iPhone|Blackberry|Android)/i", $_SERVER["HTTP_USER_AGENT"]))
 
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Perfect. I don't know if it is 'Android', but I know it is "BlackBerry" and it works great.

Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply