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 6445 times –

Version 0.2 – Includes only PHP Examples

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