Mobile Device Detection and Redirection with PHP

** Updated 04/26/2010 **  Added Google Mobile Crawling Detection
See also for additional information on Mobile Browser Detection and Redirection using Perl / CGI.

Target Audience

Web Content Publishers, Web Server Administrators and anyone considering preparing and publishing content that may be consumed by users with mobile devices.


The mobile phone and computing market is exploding. With more and more phones and hand held devices gaining web browsing capability, it only makes sense to ensure that your site is at least viewable by these mobile browsers. In this article, I present one possible solution for detecting and redirecting incoming mobile browsers so you can customize and enhance the mobile users’ experience.

In this article, I’ll discuss an example web site where we want to present customized web content to four distinct types of user based on the browser device their using when they visit the site. I’ll also discuss how to enable the users to specify a preference for one of the other versions of the site. For example, if I’m an iphone user but I really want to see the full site.

Redirecting a user based on the type of device they are using is not rocket science. In fact, it can be accomplished quite simply using an apache redirect in a .htaccess file.

The issue with this approach is that users coming to your site using an iPad will never have the ability to see the other versions of your site. for many, that may be a satisfactory solution. However, if you want users to have the ability to view a version of your site designed specifically for their type of mobile device by default – but have the option to click a link and either view or optionally set a preference for another version of the site, this article is for you.

Examples and Source Code

I know – you’re in a hurry… you’ve read the intro and you know this is what you’re after… well, here you go… The examples for this site are available for demonstration at and the code for this is available for download from the following link:

Version 0.3 – Includes PHP and Perl/CGI Examples

Download “Mobile Browser Detection and Redirection Scripts” mrlynn-MobileBrowserDetectionExample-4b68e8a.tar.gz – Downloaded 6465 times –

Version 0.2 – Includes only PHP Examples

Download “Mobile Browser Detection and Redirection Scripts” ua_mobile_redirection.tgz – Downloaded 7641 times –

21 thoughts on “Mobile Device Detection and Redirection with PHP

  1. I just want to thank you for this piece. I have not been developing for long, building a simple mobile site, this is simple, clean and clear.


    Don Booth,

  2. Good work 🙂

    But the code does not seem to pick up the iPad for some reason… Just redirects the iPad to the normal site.



  3. Solved it 🙂

    Just needed to add mobile to the end of the array on line 49 in functions.

    if(preg_match(‘/(android|up.browser||mmp|symbian|smartphone|midp|wap|phone|mobile)/i’, strtolower($_SERVER[‘HTTP_USER_AGENT’]))) {

  4. dear
    Fist of all: I”M not expert 🙁 please help

    Curently I’m using htaccess for redirect all mobile to mobile web .. but it hard to switching between two version. your PHP come up nice .. but ferpormance not realy good and it bad for my cached pages

    I looking for other solution which do cookies from htaccess

    if you can use this as reading cookies will be the best ..

    so on the cached webpages only need the link to set cookies by php ..

    thanks for any advices

  5. I really like your article, very clear.
    However, it does not seem to recognise my HTC_Touch_Pro_T7272 as a mobile device.

  6. i have a subdirectory in my root folder that is being used for a different site. is there a way to exclude this subfolder from the mobile redirect?

  7. Hi, I have a motorola 9c. I installed your php code and it worked for my desktop fine (safari). Then I tried it on my mobile 9c and it goes to the normal site. I then tried it on your uatest site, same results, but it does list the phone correctly in the information text. Please just let me know if this is because my phone (mobile windows) is not supported or something else. Thank you very much in advance. Matt Hikel

    • Matt, it’s likely that the browser on the 9c is sending a user agent string that is not recognized as a mobile device. Check the around like 45 to see where I’m evaluating the user agent string to see if it matches certain known mobile types. We may need to see what the 9c’s browser user agent string looks like and modify this line… hope this helps.

  8. Hi Mike, thanx for the repy. I solved the problem by putting ‘macintosh’ where the ‘windows’ was on line 70 and changed the variable to 1 as I pasted below. The way I look at it if the device goes through ALL the aguments for being a mobile device its got to be a NOT a mobile device by defalt, and the default is 0 set at the begining of the script.

    if (strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’mozilla’)>0) {

    I however Mike have developed another issue. All was working fine but now on one of my sites I started getting the following browser message

    Safari can’t open “expires: Sat” because Mac OS X doesn’t recognize Internet addresses starting with “expires:”.

    It did this when first installed but I keep fooling with it and it stopped doing it and was working but then it started again and will not go away. Does it everytime. BUT IT IS WORKING FINE on my other server. Got any ideas?
    Thanx in advance. Matt Hikel

    • Hmmm… sounds like there’s an error in the php script somewhere… if you’d like, I can take a look at your source – either post it somewhere where I can see it and send me a link or send it to me via email… merlynn over there at gmail dot com.

  9. Hi, ok I’m very blond when it comes to this whole script things but I need help. I have clients who want websites but also mobile site. So designing is a piece of cake but redirecting is turning out to be a nightmare for me.
    Most of my clients want their mobile site to support blackberry. What do I do? & how do I do it?

  10. Hi, so like I need help. I have clients who want websites under the condition that the sites have redirects to mobile sites that are “blackberry” compatible.
    Problems is, I don’t understand this who’ll script business. Both the main & mobile sites have been created.

  11. I have had this bookmarked for quite sometime knowing that some day I would need this information. If anyone is still monitoring this thread and they have solid knowledge of this code, can you please give me some advice? I need to implement a mobile redirect for a website but offer an option to view the full site from that mobile version. Can I use the cookie/php version above and omit the preference portion. I don’t necessarily want the user to have to set a preference…just simply be able to click “View Full Site.” Would this work? Thanks in advance to anyone willing to divulge this little secret! 😉

    • I’ve done this: Put the script on the index.thml. Mobile redirects to /m/index.html and the desktop redirects to home.html. So, if a mobile user chooses “view full site”, they go to home.html which does not have the redirect code, thus canceling a possible endless loop.

  12. On that note, I’ve used this script for a few months now. However, the client says the mobile site is not being indexed (by Google, I presume). I just learned that Google has a specific mobile crawler, and they suggest to have the “Googlebot-Mobile” User Agent added (specifically, a User agent that CONTAINS this, not MATCHES, as it may change). I am not a PHP person. Anyone know how to add this conditional statement?

  13. John,

    A most excellent question and one that points out a MAJOR shortcoming in this little example that I’ve cobbled together.

    Here’s a fix… the following are lines 49, 50 and 51 from the original version.

    if(preg_match(‘/(android|up.browser||mmp|symbian|smartphone|midp|wap|phone)/i’, strtolower($_SERVER[‘HTTP_USER_AGENT’]))) {

    To make google mobile index your mobile pages, simple replace line 49 with the following to correctly add a regex expression so that the is_mobile function recognizes google’s googlebot-mobile crawler as a mobile device.

    if(preg_match(‘/(googlebot-mobile|android|up.browser||mmp|symbian|smartphone|midp|wap|phone)/i’, strtolower($_SERVER[‘HTTP_USER_AGENT’]))) {

    I will update the package and post a new downloadable sometime later this evening.


Leave a Reply

Your email address will not be published. Required fields are marked *