CFLib.org – Common Function Library Project

isMobileBrowser(apikey)

Last updated December 18, 2008

Version: 2 | Requires: ColdFusion MX7 | Library: UtilityLib

 
Rated 4 time(s). Average Rating: 4.3

Description:
Detects whether or not user is using mobile browser by using the user-agent. Requires an API key from handsetdetection.com.

Return Values:
Returns a boolean.

Example:

view plain print about
<cfif isMobileBrowser("apikey")>
   <cflocation url="#cgi.server_name#/mobile">
<cfelse>
<!--- do nothing --->
</cfif>

Parameters:

Name Description Required
apikey Required API key. Yes

Full UDF Source:

view plain print about
<!---
 Mobile browser detector.
 v2 by Raymond Camden. Fixed var scoping, and made API key a argument.
 
 @param apikey      Required API key. (Required)
 @return Returns a boolean. 
 @author Chris Brickhouse (cbrickhouse@gmail.com) 
 @version 2, December 18, 2008 
--->

<cffunction name="isMobileBrowser" returntype="boolean" output="false">
    <cfargument name="strAPI" type="string" required="true" hint="API Key">
    <cfset var cfhttp = "">
    <!---     
    set options for http post. 
    see the handsetdetection api for more options.
    http://www.handsetdetection.com/pages/api 
    --->

    <cfset var options = "geoip, product_info, display">
    <cfset var str ="">
    <!---    
    sign up for an API key from handsetdetection.com, otherwise this will not work.
    --->

    <cfset var thisxmlok = "">
    <cfset var thisxml = "">
    <cfset var server = "http://c1.handsetdetection.com/devices/vendors.xml">
    
    <cfset str = str & "<?xml version='1.0'?>">

    <cfset str = str & "<request>">
    <cfset str = str & "<apikey>#strAPI#</apikey>">
    <cfset str = str & "<options>#options#</options>">
    <cfset str = str & "<User-Agent>#cgi.http_user_agent#</User-Agent>">
    <cfset str = str & "</request>">
    <cftry>
        <cfhttp method="Post" url='http://c1.handsetdetection.com/devices/detect.xml' resolveurl="false" timeout="10" >
            <cfhttpparam type="xml" value="#str#" >
        </cfhttp>
        <cfset thisXML = xmlparse(cfhttp.FileContent)>
        <cfset thisXMLOK = thisxml.reply.message.xmlText>
        <cfcatch type="any">
            <cfreturn false>
        </cfcatch>            
    </cftry>
    <!--- throw if bad api, don't hide it... --->
    <cfif findNoCase("Invalid API", thisxmlok)>
        <cfthrow message="#thisxmlok#">
    </cfif>
    <cfif not comparenocase(thisxmlok,"OK")>
        <cftry>
            <cfset thisXMLOK = len(thisxml.reply.product_info.is_wireless_device.xmlText)>
            <cfif thisXMLOK>
                <cfreturn true>
            <cfelse>
                <cfreturn false>
            </cfif>
            <cfcatch type="any">
                <cfreturn false>
            </cfcatch>
        </cftry>
    <cfelse>
        <cfreturn false>
    </cfif>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson