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

Version 0.2 (Older version – PHP examples only)

Download “Mobile Browser Detection and Redirection Scripts” ua_mobile_redirection.tgz – Downloaded 7602 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.

SOLVED: My WordPress Thumbnails Stopped Thumbnailing

The server where I host this blog experienced some oddness and the web server daemon (httpd) had to be restarted.  Once it came back up and it was determined that the primary DNS server on which we rely had crapped out, we were able to point to another DNS server and things seemed to get back to normal.  With a few minor exceptions that is… most notably, thumbnails were no longer thumbnailing.

This WordPress theme uses a
[sourcecode]TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks[/sourcecode]

. To diagnose the problem, the first place I looked (like every good web administrator) is the logs.
[sourcecode][Wed Jul 14 05:41:11 2010] [error] [client xx.xx.xx.xx] PHP Fatal error: Allowed memory size of 31457280 bytes exhausted (tried to allocate 12288 bytes) in /path/to/my/web/home/httpdocs/wp-content/themes/mytheme/thumb.php on line 271, referer:[/sourcecode] AHA! The problem became obvious and easy to solve. The problem appears to be that the memory_limit value in the php configuration file (php.ini) has been set to low. Sure enough it was set to 4M.

A quick edit of the php.ini file…
[sourcecode] ;;;;;;;;;;;;;;;;;;;
; Resource Limits ;

max_execution_time = 30 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 48M ; Maximum amount of memory a script may consume</pre>
And a quick restart of apache…
<pre>/etc/init.d/httpd graceful
[/sourcecode] and blam-o… we’re back in business…. thumbnail’s a thumb’n.

If you don’t have root permissions – or don’t have access to your own php.ini file, you may need to contact the support team where you host your web site. Questions? Comments?