Not yet rated

Problem

Creating a website that will be viewed by users on mobile phones such as iPhone, Blackberry etc.

Solution

Using the CGI user agent data you can get the phone type and redirect to a mobile version of your website.

Detailed explanation

The Code

First off the full code as there is not much code for such a handy way to detect and redirect the user to a mobile version.

<cfif findNoCase('blackberry', CGI.HTTP_USER_AGENT)>
<cflocation url="http://blackberry.yourdomain.com" addtoken="no">
<cfelseif findNoCase('iphone', CGI.HTTP_USER_AGENT)>
<cflocation url="http://iphone.yourdomain.com" addtoken="no">
<cfelseif CGI.HTTP_ACCEPT CONTAINS "text/vnd.wap.wml">
<cflocation url="http://wap.yourdomain.com" addtoken="no">
</cfif>

What is 'CGI.HTTP_USER_AGENT'

The CGI.HTTP_USER_AGENT is simply the output of the users computer & browser spec. If we were to use our local computers to see what gets outputted we would see something like below:

HTTP_USER_AGENT :: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5

To get this onto your screen you can use the <cfdump> tag which is one of the most handy tags you will ever use!

<cfdump var="#CGI#">

Getting our the correct details

The main bit of code that gets out the details we are after is this:

findNoCase('iphone', CGI.HTTP_USER_AGENT)

You will see first we are doing a findNoCase, this is a simple find within a string which is our CGI output and we are looking for "iPhone" you can replace iPhone with anything you want to find and redirect.

Nice and easy, give it a try! Works with CF7+ and Railo

You can download the source code for this here:

http://code.hostmediauk.com/wp-content/plugins/download-monitor/download.php?id=3


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes