PHP to detect mobile phones

Updated: 4th October 2007

The most efficient way to detect mobile phones with PHP, this simply queries the accept headers, the user agent and checks for any tell tale signs that the browser we’re sniffing is a mobile device.

This is already working on an estimated 10,000 sites, thank you everyone for the support and contributions through the comments, please note this is now released under a shared source license, a commercial license is available.

Now presented as a function with usage examples.

Download full script here[php]function detect_mobile_device(){
// check if the user agent value claims to be windows but not windows mobile
if(stristr($_SERVER[’HTTP_USER_AGENT’],’windows’)&&!stristr($_SERVER[’HTTP_USER_AGENT’],’windows ce’)){
return false;
}
// check if the user agent gives away any tell tale signs it’s a mobile browser
if(eregi(’up.browser|up.link|windows ce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp’,$_SERVER[’HTTP_USER_AGENT’])){
return true;
}
// check the http accept header to see if wap.wml or wap.xhtml support is claimed
if(stristr($_SERVER[’HTTP_ACCEPT’],’text/vnd.wap.wml’)||stristr($_SERVER[’HTTP_ACCEPT’],’application/vnd.wap.xhtml+xml’)){
return true;
}
// check if there are any tell tales signs it’s a mobile device from the _server headers
if(isset($_SERVER[’HTTP_X_WAP_PROFILE’])||isset($_SERVER[’HTTP_PROFILE’])||isset($_SERVER[’X-OperaMini-Features’])||isset($_SERVER[’UA-pixels’])){
return true;
}
// build an array with the first four characters from the most common mobile user agents
$a = array(’acs-’,'alav’,'alca’,'amoi’,'audi’,'aste’,'avan’,'benq’,'bird’,'blac’,'blaz’,'brew’,'cell’,'cldc’,'cmd-’,'dang’,'doco’,'eric’,'hipt’,'inno’,'ipaq’,'java’,'jigs’,'kddi’,'keji’,'leno’,'lg-c’,'lg-d’,'lg-g’,'lge-’,'maui’,'maxo’,'midp’,'mits’,'mmef’,'mobi’,'mot-’,'moto’,'mwbp’,'nec-’,'newt’,'noki’,'opwv’,'palm’,'pana’,'pant’,'pdxg’,'phil’,'play’,'pluc’,'port’,'prox’,'qtek’,'qwap’,’sage’,’sams’,’sany’,’sch-’,’sec-’,’send’,’seri’,’sgh-’,’shar’,’sie-’,’siem’,’smal’,’smar’,’sony’,’sph-’,’symb’,'t-mo’,'teli’,'tim-’,'tosh’,'tsm-’,'upg1′,’upsi’,'vk-v’,'voda’,'w3c ‘,’wap-’,'wapa’,'wapi’,'wapp’,'wapr’,'webc’,'winw’,'winw’,'xda’,'xda-’);
// check if the first four characters of the current user agent are set as a key in the array
if(isset($a[substr($_SERVER[’HTTP_USER_AGENT’],0,4)])){
return true;
}
}

// example 1 - detect and redirect mobile browsers
if(detect_mobile_device()){
header(’Location: http://andymoore.mobi/’);
exit;
}

// example 2 - detect and redirect desktop browsers
if(!detect_mobile_device()){
header(’Location: http://andymoore.info/’);
exit;
}[/php]Download full script here

Commercial License Details

Order here

Single site license: £10 one off fee - includes lifetime updates

Server-Wide / Multi-Site license - £50 one off fee - includes lifetime updates.

Order here - pay by PayPal

83 Responses to “PHP to detect mobile phones”

  1. Pablo Gavilan Says:

    Just great! It works perfectly and It’s what I was looking for. WURFL is too much to implement and it doesn’t works good when you just try to detect desktop vs. mobile.

    For example, browsing with Mac desktop it says it’s a Hiptop!!

    Thanks for your smart little portion of code!!

  2. Andy Moore Says:

    You’re welcome, thanks for the very positive comment! I’m pleased it’s helping and appreciated.

    The WURFL does have a place in advanced rendering but for simple handset detection the solution above is fine and very flexible.

  3. nan Says:

    Hi,

    Thanks a lot for the script, really helpfull.

    There’s a little error on line 8 about thw ‘)’ closed for substr() function. It should be:

    strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)
    );

  4. Andy Moore Says:

    Well spotted thanks, I’ve edited that line now.

  5. nan Says:

    Well, in fact it hasn’t worked for me until I modified this section (3rd test) as follows:

    $mobile_ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    $mobile_agents = array(bla bla bla...);

    foreach ($mobile_agents as $mobile_agent)
    if(strstr($mobile_ua, $mobile_agent))
    $mobile_browser++;

    I’am working under PHP 5.0.4. May be in_array functions has changed something?

  6. WirelessDuniya » Blog Archive » Quick and easy way to detect mobile phones with PHP Says:

    […] http://www.andymoore.info/php-to-detect-mobile-phones/ […]

  7. Sean Owen Says:

    I might also check for “text/vnd.wap.wml” in the Accept header, since presumably only a phone would ever ask for WML.

  8. Andrew Rickmann Says:

    This is pretty neat. I probably wouldn’t have looked this up but it seems to have fallen in my lap so I will try and find something to use it with.

    Do Google maps work on mobile devices?

  9. Andy Moore Says:

    Quote: Andrew Rickmann April 6, 2007 9:44 pm

    Do Google maps work on mobile devices?

    They sure do

  10. Cesar Says:

    Excellent script! works perfect!

    Thank you so much!

  11. Leonard Says:

    can someone point me to the java equivalent of this, please? Am more than ignorant in php

  12. MobileFeed Says:

    You really only need to check for a ‘vnd.wap.wml’ accept …..

    if(preg_match(”/vnd.wap.wml/”,$_SERVER[’HTTP_ACCEPT’])){
    // browser accepts wap so re-direct to content.
    header(”Location: http://mobile.mobi“);
    exit;
    }

  13. Andy Moore Says:

    Quote: Leonard April 11, 2007 5:09 am

    can someone point me to the java equivalent of this, please? Am more than ignorant in php

    using java the functions may be different but the logic will be the same, if you don’t want to take the juicy bits from above and build your own take a look at Luca Passani’s Switcher.

    Quote: MobileFeed April 19, 2007 5:54 pm

    You really only need to check for a ‘vnd.wap.wml’ accept …..

    Yes but that alone wouldn’t catch those requests passing a wild carded * accept value. cool site btw.

  14. JOsh Says:

    Hi there,

    I have one page that is dedicated to indie musicians and snippets of their songs available as mp3 ringtones.

    I’d like my users to be able to access the ringtones page, select their desired download link, and viola!

    My question is, with this work if I run the above PHP behind the Ringtones Page?

    What if my site usually requires a login?

    Thanks,
    Josh

  15. Andy Moore Says:

    Quote: JOsh May 9, 2007 6:57 pm

    I have one page that is dedicated to indie musicians and snippets of their songs available as mp3 ringtones.

    Hi Josh,

    That code just detects cellphones, reliably deploying content to different handsets and protecting it with digital rights management is a different story.

    If you’re looking for mobile content hosting signup here then upload your realtones and mp3 tracks to our platform.

    Once the content is hosted with us you just use our Content Gateway to generate WAP links where the band’s fans can download from.

    Apologies for the bland interface, it might look a little web -0.10 but it has some funky stuff going on the back end.

    Drop me a line if you want to know more.

    Cheers
    Andy

  16. Andy Moore Says:

    There’s an interesting new user agent out there which Russell Beattie mentions on his new Mowser blog:

    Mozilla/5.0 (iPhone; U; PPC like Mac OS X; en)
    AppleWebKit/420+ (KHTML, like Gecko)
    Version/3.0 Mobile/1A001a Safari/419.3

  17. Andrew Says:

    You still have a problem: Desktop opera browsers are detected as mobile agents because the $mobile_agents array contains ‘oper’ and Opera has this user agent: Opera/9.20 (Windows NT 5.1; U; en). The first four chars = ‘oper’.

    I could easily remove ‘oper’ from the array but the situation is so critical that I must be really sure I don’t redirect anyone to the mobile page who’s destkop… has anyone found other bugs?

  18. Andrew Says:

    Another bug: a Pocket PC with Windows Mobile 5 having this user agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240×320) is not recognized as mobile.

    Would it be a solution to include PPC in the first conditions array?

  19. Andy Moore Says:

    Quote: Andrew May 11, 2007 3:50 pm

    You still have a problem: Desktop opera browsers are detected as mobile agents because the $mobile_agents array contains ‘oper’ and Opera has this user agent: Opera/9.20 (Windows NT 5.1; U; en). The first four chars = ‘oper’.

    I could easily remove ‘oper’ from the array but the situation is so critical that I must be really sure I don’t redirect anyone to the mobile page who’s destkop… has anyone found other bugs?

    If you remove oper from the array the code will treat any Opera browser as a full browser.

    I have just added a check for $_SERVER[’X-OperaMini-Features’] as it seems that the mini version now provides some extra headers to help identify it.

    This should ensure that desktop Opera users are treated like normal browsers and Opera Mini users should be detected as being mobile browsers.

    Quote: Andrew May 11, 2007 4:00 pm

    Another bug: a Pocket PC with Windows Mobile 5 having this user agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240×320) is not recognized as mobile.

    Would it be a solution to include PPC in the first conditions array?

    I’ve also added checks for ‘windows ce’ and ‘iemobile’ in the array and a check for $_SERVER[’UA-pixels’] which is a header that Windows Mobile devices are now sending.

    As far as checking for PPC is concerned this would be a bad idea, while Microsoft suggest that it’s okay in true Microsoft style they totally play ignorant to the fact that Apple use PPC within it’s user agents to identify it’s PPC processor machines.

    (Hat tips to Mike Rowehl and Andrea Trasatti)

  20. Bleu-Rouge blog » Blog Archive » links for 2007-05-15 Says:

    […] PHP to detect mobile phones | Andy Moore (tags: mobile php wap programming tutorial wordpress) […]

  21. Pelle Boese Says:

    Awesome, Andy. Thanks for this nice snippet!

  22. Andrei Says:

    You should really check the WURFL database against your script because there still are huge bugs…

  23. Andy Moore Says:

    Quote: Pelle Boese May 18, 2007 9:08 am

    Awesome, Andy. Thanks for this nice snippet!

    Pelle, thanks for the email, I’ll get a reply back to you later.

    Quote: Andrei May 18, 2007 10:54 am

    You should really check the WURFL database against your script because there still are huge bugs…

    You miss the point, say WURLF to someone building their first mobile site and they will see a huge, complicated XML file and walk away thinking “it’s too complicate, this mobile thing isn’t for me….”

    I first saw the WURFL years back, knew it was beyond my abilities to work with then so walked away for another 18 months before I returned to it. Luckily now there are tools but still the thought of 5,000+ devices in XML format is enough to make most beginners freak out I imagine.

    The WURFL is great but firmware differences, carrier variations and carrier limitations can all return false positives.

    A bug report is only as good as the detail the user provides, if you’d care to be a bit more specific about the code above it might help me improve it.

  24. Andrei Says:

    What i was saying is that you should try to sum up the WURFL device list in your script. People using your script don’t need any details about the capabilities of the device, they only want to know if it is mobile or not. This is why you could sum up the 5000+ list into a small script(maybe a bit larger then yours) and getting close to a 100% success rate.

    I’ve built a PDA version of one of my websites and used your script to detect the device type - if it is mobile or not - (with the 2 bugs fixed) and then asked my friends to browse to the main page. They should’ve been redirected to the pda version but some of them stayed on the main. The conclusion is that until this script is working 100%, it can’t be used in production.

  25. Pelle Boese Says:

    Quote: Pelle Boese May 18, 2007 1:41 pm

    The conclusion is that until this script is working 100%, it can’t be used in production.

    Imho that ain’t true, mate. Even if only 90% of your users with mobile devices are redirected to the mobile website, it’s still better than 0. Of course you will never be able to guarantee a 100% correct matching as new devices come out like every day, but, yeah, this script could be improved. I’ll try to run WURFL against it when I’m at home this evening. Maybe I could write a script which compiles rules directly out of WURFL, I’ll inform you as soon as i got something up and working. Cheers!

  26. Andres Says:

    It works great, I almost went mad trying to detect Opera Mini. Thanks a lot!!

  27. nan Says:

    Hi! My Safari browser was detected as a mobile device and I had to delete the ‘tosh’ entry from the array.

  28. FeaT Says:

    This is sufficient
    if(preg_match(”/(wap|midp|cldc|mmp|Symbian|Smartphone)/si”,$_SERVER[”HTTP_USER_AGENT”])){
    blablabla
    }

  29. Andy Moore Says:

    Quote: nan May 21, 2007 10:50 am

    Hi! My Safari browser was detected as a mobile device and I had to delete the ‘tosh’ entry from the array.

    Hi Tosh,
    That’s odd as the tosh in the array should only match the first four characters.

    On the two Macs I have access too (my Mac Mini and brother’s iMac) Safari identifies itself like:

    iMac: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)
    AppleWebKit/312.8.1 (KHTML, like Gecko) Safari/312.6

    and

    Mini: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en)
    AppleWebKit/419 (KHTML, like Gecko) Safari/419.3

    Quote: FeaT May 31, 2007 7:20 pm

    This is sufficient
    if(preg_match(”/(wap|midp|cldc|mmp|Symbian|Smartphone)/si”,
    $_SERVER[”HTTP_USER_AGENT”])){
    blablabla
    }

    Hi Feat,

    Yes but would it be as thorough as the code above?

  30. FeaT Says:

    Sorry for my bad english google translated :) I use the code, which provided for the separation of advertising. Go to the site can be any device. For example, if a mobile phone, backed ip correct, it appears one, if not subject to the right ip, the other advertising.
    And if the browser, it displays Google Adsense.

  31. Vlad Says:

    Thanks so much, this is perfect

  32. Andy Moore Says:

    Little update, you could also check for x-xid, x-up-subno and http_via headers too as only a mobile would ever use them.

  33. shpyo Says:

    Awesome!
    It’s just what I always have ;).
    Now creating wap pages will be much interesting!!
    Thank man!

  34. Mobile Search Optimization Essentials, Optimize, Web Design, Duluth, Minnesota Says:

    […] 2 Schools of Mobile Thought There are 2 schools of thought regarding mobile site creation, double duty sites (where a single site acts as both mobile and wired) and multiple site versions. Each method can involve HTTP User-Agent headers, HTTP Accept Headers, and UAProf to identify the mobile browser and redirect to properly formatted content as appropriate. Wikipedia has a very useful list of user agents for your reference. It is possible to use.asp or PHP to detect mobile phones. […]

  35. Francois Says:

    Hey, thanks, i’m bussy with some site using mobile and pc for browsing, this is sure gonne work perfect :), thanks MR. Moore

  36. Martin Says:

    Hey this works great! Thanks!
    One small thing -> you don’t need so many brackets ;-) This has the same functionality:


    if(
    ( strpos(strtolower($_SERVER['HTTP_ACCEPT']), "text/vnd.wap.wml") > 0)
    or
    ( strpos(strtolower($_SERVER['HTTP_ACCEPT']), "application/vnd.wap.xhtml+xml") > 0 )
    or
    isset($_SERVER['HTTP_X_WAP_PROFILE'])
    or
    isset($_SERVER['HTTP_PROFILE'])
    or
    isset($_SERVER['X-OperaMini-Features'])
    or
    isset($_SERVER['UA-pixels'])
    )

  37. Francois Says:

    Hey, I know this might not be on the topic, but hey, who knows ;) Is using php and mysql okay with mobiles? Off course it will be, we just got some php code at the top… Cause the site is 80% php, I even echo the html part I just wants someones opinion. I have tested quite alot of it, and is working fine so far. Some of it is like filling a enquiry form that gets send via email, and even uploading images to the db taken by your phone. ect.

    How far can mobiles go? for php is based on server side? I know its the browser doing his thing, like the new cool Opera Mini which is available, but what about the phones default browser…

    I think I would like the ‘yes its okay’ to keep me motivated, but please give your opinions and suggestions. Especially Mr. Moore

  38. Andy Moore Says:

    Yes, PHP and MySQL with mobiles is fine, as is .net asp jsp ruby anything server side you can generate output with will be fine.

    If it works on the handset it really doesn’t matter what the output was generated with server side.

    How far can mobile go? Mobile surfing will one day overtake that of desktop surfing!

    :)

  39. Michael Says:

    Hi,
    does this technique (the http header redirect) somehow negatively influence a site’s search engine ranking??

  40. Andy Moore Says:

    Hi Michael,

    No it shouldn’t, it would only redirect a user agent if it was thought to be a mobile device so any spider like Google or mTLD doing the rounds as mobiles will see the mobile friendly version, regular spiders see regular content. The mobile spiders want mobile versions of a site, it makes sense.

    The WordPress plugin I wrote uses this code, that’s powering a lot of mobile sites now, if it caused problems I’m sure I would have been flamed to death and had my door kicked in by now.

    Have a look at Mobile Internet SEO and make sure your site has sitemaps for both web and wap version and you should be okay as long as you play withing the search engine guidelines.

  41. Jordan314 Says:

    Hiya,
    Different devices are only compatible with WML or XHTML. How can I detect which one it accepts and display only that content?

  42. Andy Moore Says:

    The WURFL is the best solution or you could query the accept headers

  43. Handi on IT and Marketing » Do you need dotMobi? Says:

    […] Since the availability of dotMobi (.mobi) last year, I have been wondering wether i need a dotMobi domain. Although the dotMobi domain have been backed by some big IT and Telecomunication company, the need to have a dotMobi is still questionable. People are already familiar with .com , .net, .org and ccTLD (country code Top Level Domain), why do you need another domain? dotMobi was marketed as an easy way for people to identified the website as a mobile-friendly website. But in my point of view having another domain for mobile user would confuse the user and i do not see any point for me to have a benefit from .mobi domain. Using subdomain for example wap.domain.com or mobi.domain.com may be enough to identified my site as a mobile friendly site. My mobile user will only need to go to domain.com and i use a WURFL script or Andy Moore’s Php script to redirect my user to the wap.domain.com or mobi.domain.com. So why do i need to waste my money to buy a new domain that will not effective. […]

  44. colin scholey Says:

    Sorry, I am really having trouble here. I am running php422 and dont know what wordpress is. I cant find a wp folder or pluggin folder. and keep getting error messeges .Need code for 4.2.2 php preferebly without the “word press stuff”
    I

  45. Andy Moore Says:

    Hi Colin,

    There is no WordPress stuff in that snip of code, all you need to do is cut and paste that into your PHP script and add a redirection to your wap site if it’s a mobile device.

    If cutting and pasting that code above gives you errors try clicking where it says ‘PLAIN TEXT’ and try that version, if it still falls over give me a heads up and I’ll do a zip download of a sample script with redirection.

  46. colin Says:

    Wow! It started working. No errors. I added an echo statement to output mobile_Browser and got 0 .wow must be the PLAIN TEXT. Cheers.

  47. colin scholey Says:

    Thanks so much for this script i have implemented it on this site, together with a couple of header redirects. I hope that as more weird devices get on the market that you would be inclined to update the above.One finel problem is with a NEC 616v mobile on the 3 network.
    It can download images(gif and jpeg) from my site(above) but damn well nothein else! Have you heard anything ?

    anyway cheers again.(ps this site does not render properly in this ie browser I can not see the first few letters of the line as i am typing - but never mind!)

  48. Jim Casey Says:

    I have been reading through this and can not figure out…

    Does this script redirect to a directory…

    i.e. http://www.djmaine.com/mobile ?

    How does it do that?

    Did I miss something?

    Thanks in advance

  49. Johann Says:

    That looks good. Generally, what is your opinion about server-side sniffing of user agents? Do do you do it yourself?

    I’ve been doing one-size-fits-all content for a while now, mostly using client-side sniffing to assign the right mobile style sheet (NetFront and handheld CSS and Linking CSS for handheld devices revisited).

  50. shobhit Says:

    Hi Andy
    i want to open my wab side on mini opera browser but it open my wap side i remove , ‘tosh’ from array but it not workrd

  51. glenn Says:

    i’m not sure how and where to the urls to direct your phone to?

    also, does this support the iphone?

    thanks.

  52. Topper Says:

    I found your code, but I am having problems getting it to work. How would I call this in my html code? Also, where does this redirect you?

    I would like to be redirected to http://www.domian.com/mobile

    Thanks for the help!

  53. Andy Moore Says:

    Quote: colin scholey July 4, 2007 6:23 pm

    I hope that as more weird devices get on the market that you would be inclined to update the above.One finel problem is with a NEC 616v mobile on the 3 network.
    It can download images(gif and jpeg) from my site(above) but damn well nothein else! Have you heard anything ?

    Yeah, it gets updated every once in a while.

    Three are running a walled garden, so damn stupid and 1993 of them but it’s the way they do it. Deploying content downloads on Three is a bit of a pain. They like to test everything on all Three phones before they’ll give it the green light.

    Quote: Jim Casey July 12, 2007 2:47 am

    Does this script redirect to a directory…
    How does it do that?

    Yes it can redirect to a directory or site of your choice, all you need to do is add the header location redirect inside the if mobile statement at the bottom.

    Quote: Johann July 22, 2007 1:22 pm

    Generally, what is your opinion about server-side sniffing of user agents? Do do you do it yourself?

    I’ve been doing one-size-fits-all content for a while now, mostly using client-side sniffing to assign the right mobile style sheet

    I feel that doing it server side is better than doing it client side as it’s one less thing for the client to worry about and have to handle. Yes, the same methods are in place on the mobile plugin I’m running on this site.

    Doing things client side gives the device more stress than needed if done server side, server side sniffing to only show the right link to the style sheet saves the device having to work it out and download all the style sheets for that document.

    Quote: shobhit July 24, 2007 2:17 pm

    i want to open my wab side on mini opera browser but it open my wap side i remove , ‘tosh’ from array but it not workrd

    Why do people think that removing tosh from the array will matter when it comes to Opera Mini? Put mini in the first pipe separated array and it should pick it up okay.

    Quote: glenn July 31, 2007 11:06 pm

    i’m not sure how and where to the urls to direct your phone to? also, does this support the iphone?

    See my comment above. Same applies to Topper.

    Yes the Apple iPhone is supported.

  54. jazz_snob Says:

    Very nice. If you wrote it as a function like detect_mobile() that returned true or false, then you could return as soon as a mobile was detected, rather than continuing thru the other if conditionals. And it would be testable.

  55. Andy Moore Says:

    Ok, this code has been bugging me for a while, I didn’t like the logic-flow so while taking jazz’s suggestion on board to make it run as a function I also took the time to make it run a little faster with a cleaner logic flow.

    Also ran some tests against the time it was taking to execute, it runs runs twice as fast as the original version.

    Thoughts and comments on version 2 please!

    Download PHP to detect mobile devices here

  56. Pablo Gavilán Says:

    One feature I find important is allowing the user to browse in desktop mode if he wants. If this detection is in normal index.php file now you need to create a different index for desktop website to redirect in case you manually want desktop, otherwise the script takes you again to mobile version.

  57. Andy Moore Says:

    But that’s more to do with the management of mobile devices once you’ve identified them than it is to do with the detection, what you suggest wouldn’t be too hard to write, just sounds like setting a session / cookie value to determine if you should over-ride it or not.

  58. Pablo Gavilán Says:

    You are right. It’s another problem, but I suggested it to make a more complete solution for desktop-mobile webs.

    I see it like multi-language websites. Autodetect language from browser, but letting the user chose a different one with a session.

  59. Andy Moore Says:

    That language stuff sounds kinda cool, I’ve been doing something similar on a mobile store building project that uses an XML based configuration file that stores all the language attributes so that any language can be applied easily to it. It also makes it highly configurable which makes it a very powerful tool I’m building. (More to come on that once it’s completed!)

    I need to do something like what you suggested on the mobile plugin for WordPress, I agree that it would make a more complete solution and once integrated into the plugin may well post the code for it here in addition to the device detection.

  60. Siddhartha Says:

    Hi,
    How can u protect content from mobile-pc browser like WAP-PROOF etc.

    thanks.

  61. terra Says:

    Hi,

    Dummies guide - What do I do with this script to use it? We have a web site (HTML), and a PHP site that spits out WML and XHTML.

    How do I use this script in its simplest form to diplay the web version of the site or the WAP version?

    Thanks!

  62. rajesh Says:

    Hello Everyone, Is there any efficient way to switch between WML pages and HTML pages when request comes from the mobile device. I am using the following code and it doesnt seem to do a good job.
    $mobileInfo=@explode(”/”,$_SERVER[’HTTP_USER_AGENT’]);

    if($mobileInfo[1]==”1.0″)
    {
    header(”location: ./wap/”);
    }
    else
    {
    header(”location:html.php”);
    }

  63. Vincent Says:

    Actually, it doesn’t work on my Palm Treo 650. I’m using the default Blazer browser. My HTTP_USER_AGENT goes as follow:

    Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; PalmSource/hspr-H102; Blazer/4.2)

    So i changed the first if for this one:

    if(stristr($_SERVER['HTTP_USER_AGENT'],'windows')&&!(stristr($_SERVER['HTTP_USER_AGENT'],'windows ce')||stristr($_SERVER['HTTP_USER_AGENT'],'palm'))){

    So it could take care of any palm device that, i guess, should have PalmSource in its header.

    I’m gonna be able to test it with a blackberry by next week.

  64. Rami Says:

    Thanks a lot . its really nice. i used it and its work correctly, thanks again…

  65. Deborah Says:

    I don’t understand why it says example one detects if it’s mobile, and then redirects to a mobile, and then then example 2 detects if it’s mobile and detects it to a desktop page. I want it to go to a mobile page, but if it cannot accept flash lite, to go to a static mobile page with no animation on it. How can I do that?

  66. Pablo Gavilán Says:

    This script is to determinate if the device is mobile or not. It can’t tell you anything about capabilities of devices. To get what you want, maybe you should try WURFL.
    Introduction: http://pc.dev.mobi/?q=node/18

  67. » Mobilní aplikace a web 2.0 - začátky » Archiv Mercadeo Blog Says:

    […] Jak poznáte, že si Vaše stránky prohlíží návštěvník v mobilním telefonu či PDA? Ve skriptovacím jazyce PHP Vám pomůže skript Andyho Moorea. Mobilní prohlížeč jste identifikovali, nyní můžete nabídnout mobilní uživatelské rozhraní (mobilní stránky). Při jeho tvorbě je třeba brát v úvahu některá specifika mobilních prohlížečů: […]

  68. PHP Lightweight device-detection at dpiNYC Literature Says:

    […] With the improvement of these new mobile web browsers, mobile web content only needs to meet the basic HTML / Javascript standards. These slight differences allow for greater creativity and less overhead in translation costs. Most developers can easily implement a switch that can present a downgraded version of the original website. The method in which they do this can vary based server configurations. Although there are several methods readily available through a google search, the code below, created by Andy Moore and updated by dev.mobi, is a great light PHP script.   […]

  69. » .htaccess Mobile Browser Redirect » Blog Archive | Oh Oh Ryan… Says:

    […] I just finished building out a simple little mobile site for hiphopdx.com. When I asked Google the best way to detect mobile browsers with PHP, it led to me this PHP code by Andy Moore. After a few minutes of contemplating the best way to include the code in the existing CMS framework I realized that it would probably be best to move the functionality outside of PHP. I mean, why clutter up display code with HTTP traffic manipulation. The website’s main traffic cop Apache is already perfectly capable of handling the duties already. […]

  70. The Gearheads » Blog Archive » GHX WebSite Update #3 Says:

    […] I was also able to add a nifty little program that was made by a man named Andy Moore. This program detects if the client that is viewing your site is using a mobile device to view your site. Now we can have a mobile edition of GHX!! But I’ll save that for later. […]

  71. موبایلتان را وردپرسی کنید! | پارسیش Says:

    […] آقای Andy Moore همچنین لطف بزرگی به جامعه کاربران وردپرس کردند و با تهیه این کد PHP امکان تشخیص و انتقال هوشمند بازدید کنندگان به نسخه موبایل را فراهم کرده اند. […]

  72. SK Blog » Adapter votre blog aux mobiles, à l’iPhone et à l’iPod Touch Says:

    […] // function to auto detect mobile phone - based on a number of methods // this is the function that works out if it’s a normal browser or a mobile browser // more info on theory behind this: <a href="http://www.andymoore.info/php-to-detect-mobile-phones/">http://www.andymoore.info/php-to-detect-mobile-phones/</a> function mobile_plugin_auto_detect(){ if(preg_match(’/(iPhone|iPod)/i’,strtolower($_SERVER[’HTTP_USER_AGENT’]))){return false;} // initialise a value at zero $mobile_browser = ‘0′; […]

  73. SV Says:

    Hi Andy,

    I’m very new in the world of making websites….I wanted to know if this plugin will work on flash sites and also if it will interfere with any search engines?

    Many thanks,

    SV

  74. xtianF Says:

    hi,
    the,”php-to-detect-mobile-phones.php”
    is not working on my server, i am running PHP Version 5.2.5 .
    i get “Internal Error 500″ fom ie7
    zip from firefox

    can give me a hint, would love the sciprt!
    -c

  75. Isaac Zein Says:

    Hello everybody,

    Can anyone help me use “PHP to detect mobile phones” code???
    Since i two websites one is for PC and the other is mobile website and i want to use this code for redirecting the websites to the proper browser like if some one type http://www.completech.com on a mobile device it should redirect it to http://www.completech.mobi mobile website and same goes with the .mobi site when typed on a PC browser…..

    Plz Help me how to use the PHP to detect mobile phones code

    Thanks

  76. sharmaji Says:

    hi andy,
    i have also tried your script for testing but this script doesn’t work for all mobiles like it detect Nokia N70, 6610,3250 but doesn’t detect Nokia N95, N80.

    i have also tried to find out the solution using wurfl to get the mobile name and version but it also doesn’t detect Nokia N80 or Nokia N95.

    if you could help me about this issue i will be very thankful to you.

    thanks
    With Warm Regards

    Sharmaji

  77. Scott Robinson Says:

    I was testing the script and came across the Blackberry 7130E and wasn’t able to get the script to work. It’s working for every other scenario tested so far. Last August you said you were testing Blackberry… Was this implemented or tested?

  78. Jochem Stoel Says:

    Hey Andy,

    Do I need specific permission to use this script in a website I am designing for a customer? I know exactly how it works and I like the code. It would be a bit pointless to rewrite the exact code and by doing that, reinventing the wheel (again).

    If I do, what are the conditions? There is a link to your website in the sourcecode, obviously.

    - Jochem

  79. Yoman Says:

    I have a social Community website. I would like to know if that code works for the social community? I want my members to be able to log in the website throught mobile. Is it possible?

  80. Gaurav Says:

    Dear Sir or Madam,

    Can it possible to know the mobile user name by phone number.

  81. Scott Says:

    I had to add “ppc” to the userAgent to get it to work with PocketPC smart phones and PDAs.

    if(eregi(’up.browser|up.link|windows ce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp|ppc’,$_SERVER[’HTTP_USER_AGENT’])) {
    return true;
    }

  82. Jim Casey Says:

    I still do not get this script. Is this a separate page?

    Should this go on a page with content?

    Help!

  83. Andy Moore Says:

    Yoman: no the code above just detects mobile browsers so you can send them to an alternative version of your site or use the info it gives you to create a more mobile specific interface.

    Gaurav - I hate being called Sir or Madam, it’s fucking ignorant so please don’t mind me returning the favour. (Yes, I’m a twat but I’m polite)

    Scott: Adding PPC will show the mobile version for most Apple Macs. Send details for the exact user agent and it’s headers and I’ll have a look.

    Jim: Store the function where you store your functions and use them where you need to use them. Explain the issue better if you want help.

    Maybe a forum would be better suited than comments….. Thoughts?

Leave a Reply