开发者

mobile detection for coldfusion

Is there a way to detection if a user is on a mobile device using just coldfusion only?

  1. I'm not doing a mobile redirect.
  2. I am using CSS media queries to change the layout to fit screen width.
  3. I just 开发者_StackOverflow社区want to do a simple if mobile render this information else render this information instead to parts of the site not the whole thing. (ex: if mobile [show jquery animation] else show [link])
  4. I don't want to waste time rendering both if only to do a display none to the info not going to be shown anyways.
  5. No javascript mobile detection (people might have javascript off on their browsers)


I used the code up at http://detectmobilebrowsers.com/ to do this. They keep the regex pretty current. The about page there has details on tablets if you need that as well as mobile. Drop if in a UDF function in order to easily keep it up-to-date on further changes.

The CFM version:

<cfif reFindNoCase("android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino",CGI.HTTP_USER_AGENT) GT 0 OR reFindNoCase("1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-",Left(CGI.HTTP_USER_AGENT,4)) GT 0>
   <cflocation url="http://mobile.yoururl.com">
</cfif>


I wrote a blog entry about this very thing!

http://www.mccran.co.uk/index.cfm/2010/6/4/Simple-Coldfusion-script-to-detect-if-a-user-is-on-a-Mobile-platform

<cfif findNoCase('Android', cgi.http_user_agent,1)>
<!--- relocate to Android version of the mobile site --->
<cflocation url="android/">
<cfelseif findNoCase('iPhone', cgi.http_user_agent,1)>
<!--- relocate to iphone version of the mobile site --->
<cflocation url="iphone/">
</cfif>

You can easily detect the browser from the user agent string. Tho as I've experienced recently browsers like chrome will allow you to change your user agent string:

http://www.mccran.co.uk/index.cfm/2011/9/25/Samsung-Galaxy-Tab-101-Switching-your-browser-from-mobile-view


Use CGI.HTTP_USER_AGENT:

<cfif (CGI.HTTP_USER_AGENT is "MobileAgentOne")>

replace MobileAgentOne with the browser you are looking for.

A quick google should provide you with an adequate list of Mobile agents currently in-use, such as: http://www.useragentstring.com/pages/Mobile%20Browserlist/

If you'd like to be a tad more robust, here's a way to provide partial matches, so you can look for families of mobile phones, rather than listing out one million different unique mobile agents:

<cfset agentList = "ASUS,AUDIOVOX,BenQ">

<cfloop list="#agentList#" index="thisAgent">
    <cfif FindNoCase(thisAgent, CGI.HTTP_USER_AGENT)>
        Found
    </cfif>
</cfloop>


You can examine the value of CGI.HTTP_USER_AGENT and see if it indicates that the client is a mobile.

Here's a quick snippet based on some Python code I found but there may be more advanced checks you can do:

<cfif refindNoCase
    ( 'android|blackberry|iphone|ipod|mobile|palm|phone|windows\s+ce'
    , CGI.HTTP_USER_AGENT
    )>
    probably mobile
</cfif>


(Apologies for making this an answer, but I'm unable to comment here.)

Testing for the string 'android' catches a bunch of tablets (motorola xoom, samsung galaxy tab, asus transformer, others from toshiba, sony, acer, etc.). Android is an OS, not a class of devices.

I'd say a realistic answer depends a lot on what you consider to be a "mobile" device. Arguably, many tablets do just fine with "normal" (non-mobile) sites, given their processing power and physical screen size, so lumping them in with much smaller and less capable mobile phones does those users a disservice.

Note that 'iPhone' is listed in several previous answers, but not 'iPad', so they implicitly exclude iOS tablets, which I believe is correct for android too.

An answer based on that approach is much less trivial, unfortunately. Various solutions are out there on the net, one basic version of which is to look for the word mobile and exclude some tablet-specific strings such as tablet, pad, xoom, eeepc, flyer, etc. However, new devices appear every week, and chasing even the most popular ones will be a challenge at best.


Does the detectmobilebrowsers.com solution go in the header? Is there any reason not to put the detection line before the header? I have a frameset (I know, not ideal...) and I think the frameset is getting detected first and loading my frames before running the detection script runs. (Is that possible?) It works fine in the header with android, but not kindle, ipad or iphone (I added an ipad line).

Thanks for reading, LPP


(Sorry for answering instead of commenting, I'm not able to comment or vote up)

I initially liked mujimu's solution. I put it on my site and it seemed to work well testing with my own iphone. However, I started testing with this http://www.mobilephoneemulator.com/ and it didn't work for a majority of devices.

Here's what I would do (and will do). Output something like this in a discreet location <cfoutput>#CGI.HTTP_USER_AGENT#</cfoutput>. Then test and record what the devices output.

Then, use something like this (thank you Peter Boughton) and in between the pipes, use a distinct keyword from the testing...

<cfif refindNoCase
    ( 'distinct_keyword|android|blackberry|iphone|ipod|mobile|palm|phone|windows\s+ce'
    , CGI.HTTP_USER_AGENT
    )>
    probably mobile
</cfif>

After my initial testing here's the list I've come up with. Please let me know if you think this is accurate...

ARCELIK,Access,Acer,Ahong,Aiko,Airis,Airness,Alcatel,AlphaCell Wireless,Amazon,Amoi,Amoisonic,Android,AnexTek,Apple,Archos,Asmobile,Aspen,Asus,ASUSTeK COMPUTER INC.,Audiovox,Becker,BEKO,Bellawave,BellWave,BenQ,BenQSiemens,BenQ-Siemens,Bird,Bleu,Blue Coat,B-Mobile,Bolt,Bytemobile,Capitel,Casio,CDM,CECT,CheckCom,Cingular,Cking,Compal,Compaq,CoralWeb,Cricket,Dai Telecom,Dallab,Danger,Daxian,DBTEL,Dicam,Dmobo,DoCoMo,Dopod,Doris,DreamPhone,EDL,Elite,ELSON,Emblaze,Emobile,Enteos,Era,Ericsson,Ericy,ETEN,EZIO,EZZE,Fitel,Fly,Fujitsu,generic,generic web browser,Generic Windows,Generic Windows Mobile,GIGABYTE,Gionee,Google,Gradiente,Grundig,Haier,Handspring,Hedy,Hei,Helio,Hewlett-Packard,Hisense,Hitachi,HP,HTC,HTC Corporation,HTIL,HUAWEI,Hummer,IAC OKWAP,IAC OKWAP Mobile Communications,Idea,iKoMo,IM,Imate,i-mate,i-mobile,Infineon,InfoSonics,Innostream,INQ Mobile,Itelco,IXI,KCM,KDDI,KDDI - SonyEricsson,KDDI-Casio,KDDI-Hitachi,KDDI-Kiocera,KDDI-Kyocera,KDDI-Panasonic,KDDI-Pantech,KDDI-Sanyo,KDDI-Sharp,KDDI-SonyEriccson,KDDI-SonyEricsson,KDDI-Toshiba,Kejian,Kisen,Klondike,KONKA,Kozi,KPT,K-Touch,Kyocera,Kyocera Wireless Corp,LCT,Lenovo,Lenovo Mobile,Lexibook,Lexus,LG,LG Electronics,LGE,Lobster,Longcos,LT,LXE,M3 Gate,Malata,MAUI-based Generic,Maxon,Medion,MicroMax,Microsoft,MIO,Mio Technology,Miracle,MiTAC,Mitsubishi,Mobile Wireless Group,MOBISTEL,Modelabs,ModelLabs,Modottel,Modu,MOMO Design,Morange,Motorola,Myphone,NEC,Neonode,Newgen,Nexian,Nintendo,Nokia,Novarra,NTT DoCoMo,NTT DoCoMo FOMA 3G,O2,ONDA,Openwave,Opera,Optimay,Orange,Palm,PalmOne,Panasonic,Panda,Pantech,PCD,Philips,PhoneOne,Pirelli-Arcor,Porsche Design,Porshe,Poseidon,Psion,QCI,QMobile,Qtek,Raks,Reporo,Research In Motion Ltd.,RIM,Robot,Ron,Rover,RT,Sagem,SAMART,Samsung,Sanyo,SavaJe,SEC,Semc,Sendo,Sharp,Siemens,Skyfire,Skyspring,Skyspring Vitelcom,Skyworth,SKYZEN,SlobTrot Software,SmartTrust,Smile,SoftBank,Sonim,Sony,Sony Ericsson Mobile Communications,SonyEricsson,Spice,Sprint,SPV,Sunrise,TCL,Techfaith,Tel.Me.,Telecom Italia,Telit,TIANYU,T-Mobile,Toplux,Toshiba,Tranxcode,TTPCom,tvCompass,tvCompass DMR1500,Ubiquam,UCWEB,Uniscope,Unistar,Uriver,Usha Lexus,Utec,UTStarcom,V3m (Metro PCS),Vacom,Velocity Mobile,Venera,Verizon,Vertu,VeryKool,VERZIOWORLD,Vitelcom,VITELCOM for Telefonica Movistar,VK,VK Mobile,Vodafone,Vodafone ZTE,Voxtel,W3C,Wapamp,Wapsilon,WAPUniverse,WellcoM,WinWAP Technologies,Wonu,XDA,Yas,Yospace Ericsson,Yospace Motorola,Yospace Nokia,Yospace Sharp,Yospace Siemens,Yospace SonyEricsson,Yulong,Zonda,ZT,ZTE,.mobi


Per the following JavaScript answer, a better solution may now be to use something like the following, which avoids issues with changing user agents.

https://stackoverflow.com/a/24600597/1427373

Adapted for ColdFusion:

<cfset mobileDevice = false>

<!--- Detect if we are using a mobile device --->
<cfif reFindNoCase("Mobi|Android", CGI.HTTP_USER_AGENT) NEQ 0>
  <cfset mobileDevice = true>
</cfif>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜