Welcome to WebmasterWorld Guest from 54.147.20.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

detectar iphon, ipad etc con php

     

helenp

6:12 pm on Feb 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,
I want to serve a diferent menu to all touchscreens,
from smartphones to tablets.
I found many ways to detect the smartphones using php, javascript or css.
As far as I have seen in the shop the tablets is ipad or use Android, however maybe this is not so.
So anyboydy know if its posible to detect all device that are touchscreen using php, wich to me seems to be the best way.

Adamphp

8:53 pm on Feb 7, 2012 (gmt 0)



I think java script is best for what you trying to Achieve

This is what i use for iPhone/iPad detection

<script type="text/javascript">
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
document.location = "mobile/";
}
</script>

rocknbil

3:25 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Neither is really the "best way," especially Javascript. Mobile phones return us to the 90's when bandwidth is *really* important, many users have to pay for bandwidth, and each cycle of memory eats both battery and bandwidth. I can read on my Android what's using the most battery, and almost see it drain on heavy sites.

The best way IMO is to detect it at a server level by user agent. On linux with .htaccess:

RewriteCond %{HTTP_USER_AGENT} android.+mobile|blackberry|iphone|ipod|ipad [NC]
RewriteRule ^$ /mobile-phone-site.php [L]

But, in your case, if it's **only** the menu you want to modify (I'm guessing it's not, as other parameters need to be modified for mobile,) You can use the same method in PHP:

$ua = $_SERVER['HTTP_USER_AGENT'];

switch ($ua) {
case preg_match('/android.+mobile/i'):
// Android menu
break;
case preg_match('/iphone|ipod/i'):
// iphone/ipod menu - they're generally the same
break;
case preg_match('/ipad/i'):
// ipad, whole new world
break;
case preg_match('/blackberry/i'):
// Even these have different handling depending on model
default:
// normal browsers
}


That's of course really incomplete, as it will only address those devices. To pander to all mobile browsers, back to the 90's again, with a long list of UA's . . .

lucy24

5:05 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



When you say .+ in "android.+mobile" what do you really mean? A few nanoseconds of server time translates to a few nano-somethings of battery life ;)

helen, I like your thread title. "Oops, what language was this forum in again?"

rocknbil

4:30 pm on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It means what it means . . . one or more of any character. Sometimes a space, sometimes a version defined via characters . . .

lucy24

11:13 pm on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



The question is: can it be constrained more tightly? We're trying to avoid letting the Regular Expression race all the way to the end of the UA and then have to backtrack: "Whoops, I was supposed to pick up that 'Mobile' back there."

:: detour to recent logs ::

Holy ###, what a lot of different Androids there are. Case sensitive, as is "Mobile", but that's about all you can say. There can be absolutely anything between "Android" and "AppleWebKit/[\d.]+\+? \(KHTML, like Gecko\) Version/[\d.]+ Mobile". About all you can do is say .+? instead of .+

... until you get to Android 3, which doesn't say Mobile at all. Is it mobile, or is it some unrelated device?

penders

11:37 am on Feb 9, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



switch ($ua) {  
case preg_match('/android.+mobile/i'):
// Android menu
break;
:


For anyone copy 'n' pasting, this is pseudo (PHP) code :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month