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
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
April 3rd, 2007 at 8:51 pm
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!!
April 3rd, 2007 at 10:26 pm
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.
April 5th, 2007 at 10:52 am
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)
);
April 5th, 2007 at 11:11 am
Well spotted thanks, I’ve edited that line now.
April 5th, 2007 at 11:29 am
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?
April 6th, 2007 at 7:31 am
[…] http://www.andymoore.info/php-to-detect-mobile-phones/ […]
April 6th, 2007 at 2:50 pm
I might also check for “text/vnd.wap.wml” in the Accept header, since presumably only a phone would ever ask for WML.
April 6th, 2007 at 9:44 pm
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?
April 6th, 2007 at 10:43 pm
Quote: Andrew Rickmann April 6, 2007 9:44 pm
They sure do
April 8th, 2007 at 7:02 am
Excellent script! works perfect!
Thank you so much!
April 11th, 2007 at 5:09 am
can someone point me to the java equivalent of this, please? Am more than ignorant in php
April 19th, 2007 at 5:54 pm
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;
}
April 20th, 2007 at 2:57 am
Quote: Leonard April 11, 2007 5:09 am
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
Yes but that alone wouldn’t catch those requests passing a wild carded * accept value. cool site btw.
May 9th, 2007 at 6:57 pm
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
May 10th, 2007 at 12:18 am
Quote: JOsh May 9, 2007 6:57 pm
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
May 11th, 2007 at 12:35 pm
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
May 11th, 2007 at 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?
May 11th, 2007 at 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?
May 11th, 2007 at 7:04 pm
Quote: Andrew May 11, 2007 3:50 pm
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
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)
May 15th, 2007 at 4:30 am
[…] PHP to detect mobile phones | Andy Moore (tags: mobile php wap programming tutorial wordpress) […]
May 18th, 2007 at 9:08 am
Awesome, Andy. Thanks for this nice snippet!
May 18th, 2007 at 10:54 am
You should really check the WURFL database against your script because there still are huge bugs…
May 18th, 2007 at 11:39 am
Quote: Pelle Boese May 18, 2007 9:08 am
Pelle, thanks for the email, I’ll get a reply back to you later.
Quote: Andrei May 18, 2007 10:54 am
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.
May 18th, 2007 at 1:10 pm
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.
May 18th, 2007 at 1:41 pm
Quote: Pelle Boese May 18, 2007 1:41 pm
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!
May 21st, 2007 at 1:36 am
It works great, I almost went mad trying to detect Opera Mini. Thanks a lot!!
May 21st, 2007 at 10:50 am
Hi! My Safari browser was detected as a mobile device and I had to delete the ‘tosh’ entry from the array.
May 31st, 2007 at 7:20 pm
This is sufficient
if(preg_match(”/(wap|midp|cldc|mmp|Symbian|Smartphone)/si”,$_SERVER[”HTTP_USER_AGENT”])){
blablabla
}
June 1st, 2007 at 12:18 pm
Quote: nan May 21, 2007 10:50 am
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
Hi Feat,
Yes but would it be as thorough as the code above?
June 1st, 2007 at 12:44 pm
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.
June 2nd, 2007 at 1:28 am
Thanks so much, this is perfect
June 4th, 2007 at 5:03 am
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.
June 5th, 2007 at 9:59 pm
Awesome!
It’s just what I always have ;).
Now creating wap pages will be much interesting!!
Thank man!
June 6th, 2007 at 4:09 pm
[…] 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. […]
June 22nd, 2007 at 2:47 pm
Hey, thanks, i’m bussy with some site using mobile and pc for browsing, this is sure gonne work perfect :), thanks MR. Moore
June 22nd, 2007 at 3:25 pm
Hey this works great! Thanks!
This has the same functionality:
One small thing -> you don’t need so many brackets
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'])
)
June 24th, 2007 at 10:51 am
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
June 26th, 2007 at 3:09 pm
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!
June 27th, 2007 at 12:00 pm
Hi,
does this technique (the http header redirect) somehow negatively influence a site’s search engine ranking??
June 27th, 2007 at 1:03 pm
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.
June 27th, 2007 at 9:03 pm
Hiya,
Different devices are only compatible with WML or XHTML. How can I detect which one it accepts and display only that content?
June 27th, 2007 at 9:22 pm
The WURFL is the best solution or you could query the accept headers
June 30th, 2007 at 10:22 am
[…] 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. […]
June 30th, 2007 at 3:12 pm
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
June 30th, 2007 at 3:46 pm
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.
July 2nd, 2007 at 5:01 pm
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.
July 4th, 2007 at 6:23 pm
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!)
July 12th, 2007 at 2:47 am
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
July 22nd, 2007 at 1:22 pm
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).
July 24th, 2007 at 2:17 pm
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
July 31st, 2007 at 11:06 pm
i’m not sure how and where to the urls to direct your phone to?
also, does this support the iphone?
thanks.
August 2nd, 2007 at 7:29 pm
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!
August 2nd, 2007 at 11:34 pm
Quote: colin scholey July 4, 2007 6:23 pm
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
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
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
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
See my comment above. Same applies to Topper.
Yes the Apple iPhone is supported.
August 12th, 2007 at 5:51 pm
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.
August 14th, 2007 at 5:39 pm
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
August 14th, 2007 at 5:50 pm
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.
August 14th, 2007 at 5:59 pm
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.
August 14th, 2007 at 6:30 pm
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.
August 14th, 2007 at 6:37 pm
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.
August 19th, 2007 at 10:55 pm
Hi,
How can u protect content from mobile-pc browser like WAP-PROOF etc.
thanks.
August 20th, 2007 at 4:48 pm
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!
August 24th, 2007 at 2:57 pm
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”);
}
August 27th, 2007 at 10:14 pm
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.
August 30th, 2007 at 9:32 pm
Thanks a lot . its really nice. i used it and its work correctly, thanks again…
September 4th, 2007 at 9:31 pm
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?
September 5th, 2007 at 12:08 am
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
September 11th, 2007 at 9:20 pm
[…] 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čů: […]
September 21st, 2007 at 6:01 am
[…] 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. […]
October 30th, 2007 at 3:48 am
[…] 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. […]
January 18th, 2008 at 5:44 pm
[…] 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. […]
March 14th, 2008 at 5:19 pm
[…] آقای Andy Moore همچنین لطف بزرگی به جامعه کاربران وردپرس کردند و با تهیه این کد PHP امکان تشخیص و انتقال هوشمند بازدید کنندگان به نسخه موبایل را فراهم کرده اند. […]
April 6th, 2008 at 12:45 am
[…] // 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′; […]
April 14th, 2008 at 1:51 am
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
April 14th, 2008 at 8:39 pm
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
April 15th, 2008 at 5:06 pm
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
April 15th, 2008 at 5:37 pm
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
April 16th, 2008 at 8:53 pm
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?
April 17th, 2008 at 11:14 am
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
April 20th, 2008 at 4:02 am
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?
May 9th, 2008 at 12:02 pm
Dear Sir or Madam,
Can it possible to know the mobile user name by phone number.
May 9th, 2008 at 4:51 pm
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;
}
May 12th, 2008 at 6:00 pm
I still do not get this script. Is this a separate page?
Should this go on a page with content?
Help!
May 16th, 2008 at 1:33 am
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?
May 22nd, 2008 at 5:23 pm
I’m having the same PHP version issue xtianF has. Is there any new information on the script working correctly on PHP 5.2.5?
May 30th, 2008 at 7:46 am
Hey Andy,
thanks for your Script, but i have a problem with the script.
If i take your script as it is, my site shows up with this error here:
Fatal error: Cannot redeclare detect_iphone() (previously declared in domain.com-:171) in domain.com- on line 194
(domain.com is replaced)
and when i am deactivate this area:
function detect_iphone(){
if(eregi(’iphone’,$_SERVER[’HTTP_USER_AGENT’])){
return true;
}
}
It works on my Desktop, but the iphone Shows that it is a mobile device and not an iphone.
Would you help me out?
Thanks.
June 6th, 2008 at 12:12 am
This is excellent !!!! Its so simple and straightforward.
I just had to add couple of lines for iphone and ipod.. but good enough to kick off..
Thanks Andy..
June 6th, 2008 at 6:41 pm
hey can u guide me how to apply this code and where
June 7th, 2008 at 8:22 am
[…] This plugin will automatically detect most modern mobile phones & PDAs (including PlayStation Portable) then show them a mobile friendly version of your site. More info on the mthod used: Detecting mobile phones with PHP. […]
June 8th, 2008 at 5:58 am
HI Andy,
this is out of topic.. im asking if in any case?. you know how to embed a google map version into a mobile site?.. a quick reply would be appreciated.
thanks
June 10th, 2008 at 6:43 pm
Why the hell do you want people to pay for your little snippet that you put together from other peoples code.
Cannot figure out why?
June 10th, 2008 at 8:21 pm
After reading your site a little more and looking at your code.I love this remark:
“…Cameron had the decency to ask if he could publish my work, many others haven’t and the last folks that used it on their software without any reference to me claim to be powering over half a million sites. While I’m a bit pissed there’s no credit there to me I definitely think I can say I’m doing my bit for the mobile web.”
You built a php function, with an array in it..
And you want credit for that.
Why don’t you give credit to the php writers that gave you those nice OPEN SOURCE FUNCTIONS that you make $$ off of.
Exactly how long did you spend on this??
You had to do some typing while making the array…
I have seen other examples of this code so you are not the first.
So I am still not sure why you are so amped about people using your 150 lines of php.
I love this remark “I can say I’m doing my bit for the mobile web”..
I will dust off the “mobile developer of the year trophy….”
For the record I have been a developer for over 7 years (read the sarcasm).
I am a CSS-XHTML coder. Using Jquery and lots of fun things from php. I have developed Drupal sites….
Guess I had to ruffle my feathers..
Stick to wordpress it is easy for “developers” such as yourself.
June 14th, 2008 at 12:31 pm
I wish to find the same script but that it directed Samsung on separate page! Where I must to find it? Or how to change this script to make it?
June 15th, 2008 at 3:54 pm
Awesome, Andy. Thanks for this nice snippet!
July 1st, 2008 at 2:32 am
I have a palm Treo and tried placing your code on my website. The code did not detect my palm as being on a mobile. The web browser is Blazer. Now what?
July 6th, 2008 at 11:10 pm
Andy, should the code be inserted WITHIN the after the META, SCRIPT, etc…? Or is it better off outside the ? Is closest to the top of the code the best option?
Thanks for your time. Looks like an awesome script.
Diab
July 10th, 2008 at 7:29 pm
I can’t get this to work on my site. When I try to load the site on a mobile device it just goes to the regular website.
Site - http://www.cooneyrobotics.com/
Mobile - http://www.cooneyrobotics.com/mobile/
What am I doing wrong?
July 11th, 2008 at 5:05 am
Can anyone help me in developing mobile e-banking using php???
How can i develop a system using webservice? wsdl? nusoap? php mobile? java mobile? php soap?
July 11th, 2008 at 10:45 am
Maybe include separate check for PPC to avoid Mac being detected as mobile browser, like if(eregi(’ppc’,$_SERVER[’HTTP_USER_AGENT’]) && !eregi(’mac’,$_SERVER[’HTTP_USER_AGENT’]))
July 12th, 2008 at 2:33 pm
I need a script to redirect users with Nokia to seperate page, SE - to separate page too, and Samsungs too. Please, write this script!
July 14th, 2008 at 1:17 pm
I just ported your function yo JSP.
It works great.
But i found an bug using a BlackBerry8700:
User Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) BlackBerry8700/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/175
Your script thinks that this UA claims to be windows(but not windows mobile), but it doesn’t.
Solution: I exchange the first “if” position to the second and now it matches “midp” so it’s detected to be a mobile.
What do you think?
Regards
rjarl
July 17th, 2008 at 2:55 am
it’s work !!…
nice script mr. andy
do u hv another script that running on mobile ??
i’m interesting open source ..
thx mr. andy, keep share …
July 18th, 2008 at 9:50 am
Script works good but i have a question.. when a mobile connects to the website it is redirected to the mobile version, BUT if the client wants to see the full version? for example, the client beeing in the mobile version clicking a link to see full version, how can i bypass the mob check??? any ideas? Im running the script on a Joomla 1.5.3 framework
July 19th, 2008 at 8:25 pm
Dxwolf. The way I would do it is to make another index page that doesn’t contain the script. That way when someone clicks to go to the full site. Just make sure that all of your internal links don’t point back to the index page with the script in it.
So you would really only use the index page with the detector like a one way street that has a fork in the road. Use that script page like the cheap hooker you need her to be and don’t ever link back. Until you need her again.
If I just confused you, so sorry. But all bullsh*t aside,that is how I would structure my page.
July 21st, 2008 at 12:52 am
@ dxwolf. In this case, I would create another index page (or home page) that doesn’t contain the PHP Script. Just make sure that any links referring to the index/home page aren’t referring to the one that contains the script. So once they click “View PC Version” they would get redirected to index2.php and all internal links would point back to index2.php instead of index.php. Then on the main site, your link to “View Mobile Site” links to index.php and the script can do its magic there.
Did I make sense? I hope so.
July 27th, 2008 at 11:20 pm
I pasted the code into the top of my regular home page, http://www.mrtownhome.net, but my Blackberry will not redirect to my mobile.mrtownhome.net web page. All I see is the code at the top. What am I missing?
July 28th, 2008 at 12:24 am
Does the script work for palm centro? i can get the redirect to main page but not to mobile page.
http://amkofence.com/amko7.php is where i am testing it
July 30th, 2008 at 9:01 am
[…] und performant. Beispiele für eine Browserweiche wären zum Beispiel die “mobile phone detection” von Andy Moore (engl.) oder das “Mobile Detection Kit” von Eugenia Loli-Queru […]
July 31st, 2008 at 1:14 pm
Could someone please provide a short “How-To” regarding installation of this script. Also, I noticed the script was last updated 28 February 2009 (2009).
Thanks,
Frank
August 7th, 2008 at 12:10 am
This is just what I was looking for to incorporate into my website Thrice Scene. This has saved me a little bit of time coding up one myself, nicely coded.
August 13th, 2008 at 12:13 pm
Perfect!
Thanks!
August 13th, 2008 at 12:24 pm
Andy,
I got one error in your script.
two functions, which one I should remove.
function detect_iphone(){
if(eregi(’iphone’,$_SERVER[’HTTP_USER_AGENT’])||eregi(’ipod’,$_SERVER[’HTTP_USER_AGENT’])){
return true;
}
}
function detect_iphone(){
if(eregi(’iphone’,$_SERVER[’HTTP_USER_AGENT’])){
return true;
}
}
August 15th, 2008 at 1:39 am
[…] Using PHP to detect the iPhone […]