homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

detectar iphon, ipad etc con php

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

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.



 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/";


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

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:


switch ($ua) {
case preg_match('/android.+mobile/i'):
// Android menu
case preg_match('/iphone|ipod/i'):
// iphone/ipod menu - they're generally the same
case preg_match('/ipad/i'):
// ipad, whole new world
case preg_match('/blackberry/i'):
// Even these have different handling depending on model
// 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 . . .


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

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?"


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

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


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

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?


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

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

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved