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 module.

Download Code and Examples Here:
From Github:

or locally:
Version 0.3 (Latest – includes perl/cgi AND PHP examples)

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

Version 0.2 (Older version – PHP examples only)

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

View the working example using Perl/CGI here:

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 ^(.*)$ [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 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 make use of %ENV and module variables such as param(). Consider the following table which contrasts PHP and Perl/CGI.

3 thoughts on “Mobile Device Detection and Redirection with Perl and CGI

  1. thank you for this. i’d like to point out that, while i believe it easier to set up a basic website using php, the differences in say, setting up cookies are not as stark as it might seem from this article. for example, while one may create a cookie with a php one-liner, try putting any of those arguments in the ‘wrong’ order, or leaving any one of them out. being verbose can be very helpful.

    again, very nice article, and, thank you!

Leave a Reply

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