Mobile Device Detection and Redirection with Perl and CGI

Last month, I took some time and wrote about Mobile Device Detection and Redirection with PHP. Well, it turns out that some folks can’t (or choose not to) make use of PHP and favor the Perl/CGI approach to web development. This article covers the same bases and presents a set of examples implemented using Perl and Lincoln Stein’s great CGI.pm module.

Download Code and Examples Here:
From Github: http://github.com/mrlynn/MobileBrowserDetectionExample

or locally:
Version 0.3 (Latest – includes perl/cgi AND PHP examples)
Mobile Browser Detection and Redirection Scripts (6390)

Version 0.2 (Older version – PHP examples only)
Mobile Browser Detection and Redirection Scripts (7569)

View the working example using Perl/CGI here: http://mlynn.org/uatest/cgi-bin/index.cgi

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.
[sourcecode] RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301] [/sourcecode]

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.

Perl and CGI have been around for many years and are in wide use on the web. Perl is extremely extensible and using the very popular CGI.pm Perl Module written by Lincoln Stein, it’s very easy to write code to dynamically manage your web site.

Where PHP has built-in variables designed to expose the server and apache session environment ($_SERVER, $_SESSION), Perl and CGI.pm make use of %ENV and module variables such as param(). Consider the following table which contrasts PHP and Perl/CGI.

Mobile Device Detection and Redirection with PHP

** Updated 04/26/2010 **  Added Google Mobile Crawling Detection
See also http://www.mlynn.org/2010/07/mobile-device-detection-and-redirection-with-perl-and-cgi/ 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.

Overview

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 http://mlynn.org/uatest/ and the code for this is available for download from the following link:

Version 0.3 – Includes PHP and Perl/CGI Examples
Mobile Browser Detection and Redirection Scripts (6390)

Version 0.2 – Includes only PHP Examples
Mobile Browser Detection and Redirection Scripts (7569)