Detect Mobile User Agents & Browsers
Helping detect and better serve mobile browsers
Web site last updated: Wednesday 23rd December 2009
Function last updated: Wednesday 23rd December 2009
This PHP function lets you choose how to manage your mobile visitors, they can be redirected to a page built for mobiles or you can use it to decide which markup language and stylesheet to show that user.
It's main function is to answer this question:
Is this visitor to my website using a mobile phone or PC?
Download Code
Download the code, unzip the archive then upload it to your website. Then view the usage guide or use the function generator to build a custom function to suit you and your mobile visitors.
- Download the code here - downloaded 75,680 times
Usage guide
It's not rocket science, it's easy to use!
But you need to understand at least basic PHP to get it working.
You need to include the file with the function then call the function before your PHP pages do anything else. See Support.
include('mobile_device_detect.php');
$mobile = mobile_device_detect();
The function has eight parameters that can be passed to it which define the way it handles different scenarios. These paramaters are:
- iPhone - Set to true to treat iPhones as mobiles, false to treat them like full browsers or set a URL (including http://) to redirect iPhones and iPods to.
- Android - Set to true to treat Android handsets as mobiles, false to treat them like full browsers or set a URL (including http://) to redirect Android and Google mobile users to.
- Opera Mini - Set to true to treat Opera Mini like a mobile, false to treat it like full browser or set a URL (including http://) to redirect Opera Mini users to.
- Blackberry - Set to true to treat Blackberry like a mobile, false to treat it like full browser or set a URL (including http://) to redirect Blackberry users to.
- Palm - Set to true to treat Palm OS like a mobile, false to treat it like full browser or set a URL (including http://) to redirect Palm OS users to.
- Windows - Set to true to treat Windows Mobiles like a mobile, false to treat it like full browser or set a URL (including http://) to redirect Windows Mobile users to.
- Mobile Redirect URL - This should be full web address (including http://) of the site (or page) you want to send mobile visitors to. Leaving this blank will make the script return true when it detects a mobile.
- Desktop Redirect URL - This should be full web address (including http://) of the site (or page) you want to send non-mobile visitors to. Leaving this blank will make the script return false when it fails to detect a mobile.
Function Generator
To make the function easier to work with from a human's perspective use the function generator to select the way you'd like the code to handle different mobile devices.
Just complete the form then click the 'Generate function' button. Your custom function call will be displayed below.
Upgrade
Please upgrade for use on commercial websites
This code is free to download and use on non-profit websites, if your website makes money, shows adverts or is in any way transactional please kindly upgrade and help me keep the code coming!
Please upgrade for commercial usage
A testimonial from the company who made my kitchen:
![]() |
Lee @ Howden Joinery www.howdens.com I discovered your website yesterday, and like the idea of your mobile re-direct code . . . as it saves my developers days and days of work! I have already implemented the code on our company website (www.howdens.com) and everything is working perfectly! |
Some very lovely and happy clients:
- Calorie Counter Database - about.com
- Punchkick Interactive - punchkickinteractive.com
- Clearwave Mobile - clearwavemobile.com
- Stratus Creative Studio - stratuscreativestudio.com
- Bridge 9 Records - bridge9.com
- Sesame Vault Web Video Engine - sesamevault.com
- U Have Audio - uhaveaudio.com
- Legal Mp3 Downloads - officialmp3s.com
Change Log:
- 25.11.08 - Added Amazon's Kindle to the pipe seperated array
- 27.11.08 - Added support for Blackberry options
- 27.01.09 - Added usage samples & help with PHP in HTML - .zip
- 09.03.09 - Added support for Windows Mobile options
- 09.03.09 - Removed 'ppc;'=>'ppc;', from array to reduce false positives
- 09.03.09 - Added support for Palm OS options
- 09.03.09 - Added sample .htaccess html.html and help.html files to download
- 16.03.09 - Edited sample .htaccess file - now works with GoDaddy
- 14.08.09 - Reduced false positives
- 14.08.09 - Added Palm Pre
- 14.08.09 - Added answer about search engine spiders
- 14.08.09 - Added status variable to report back it's findings for debugging
- 14.08.09 - Added Torch Mobile Iris Browser to Windows Mobile section
- 14.08.09 - Added HTC Touch 3G to Windows Mobile section
- 14.08.09 - Added help links to PHP header and setup PHP in HTML
- 14.08.09 - Added six usage examples
- 15.08.09 - Checked against the list of agents in the WURFL - 99.27% detected!
- 11,489 mobile user agent strings checked
- 99.27% detection rate after a number of small changes
- Those user agent strings listed that are not detected are either robots or too generic for user agent detection
- Any mobiles not detected by their user agent would most likely return true as they'd be detected by the headers they add.
- 20.11.09 - Removed PDA from the piped array to stop false positives
- 22.12.09 - Moved the site to a server hosted at Rackspace
- 23.12.09 - Added support for Mozilla Fennec
Support
Most common questions:
- Warning: Cannot modify header information - headers already sent by...
- How do I make this work on my HTML pages?
- Can you help with samples of PHP in HTML?
- Is it free to use this code?
- Can I use this code on a commercial site?
- I've upgraded can I use this code on all my sites?
- How do I upgrade?
- How do I test it works?
- WHAT ABOUT SEARCH ENGINES?
Requirements
A desire to give mobile users a better experience, a web server or web hosting environment which has support for PHP, a few minutes and a coffee. More coffee, or is that just me?
I've done everything I can to make this as simple as possible but you do need at least a basic understanding of PHP or a willingness to dive into the PHP Manual to get it working. Please understand I can't assit you at all if you do not have some skill with PHP. It's like trying to teach a cat to bark.
Report a bug
Please submit bug reports to andy at andymoore.info - please also get in touch with any suggestions for the code or about the use of this website.
Contributers are rewarded! If you email me to suggest a new feature and I use it I'll send you something through PayPal to say thank you.
If a device is detected as mobile when it isn't or vice-versa please email me with the user agent that is causing the problem and I will issue an update to the code. Please include the words FALSE POSITIVE in the subject.
Your current user agent is: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
