使用Coldfusion获取浏览器和版本

时间:2011-06-09 14:59:54

标签: coldfusion user-agent

Coldfusion中是否有内容可以让我知道浏览我网站的浏览器和版本号?是否存在能让我这样做的存在的库?

除了简单的if语句检测IE之外,我的google-fu还没有显示很多其他内容。

我确实在Chrome中转储了cgi.http_user_agent变量:

  

Mozilla / 5.0(Windows NT 5.1)AppleWebKit / 534.24(KHTML,与Gecko一样)Chrome / 11.0.696.77 Safari / 534.24

那里提到了三种不同的浏览器!执行if语句寻找其中一个将返回true。必须采用更正式的方法吗?

8 个答案:

答案 0 :(得分:5)

你能不能使用javascript代替? http://cssuseragent.org/

CSSUA非常棒,为标签添加了简单的类,并允许您使用ua-ie-6等类在CSS / Javascript中定位内容

答案 1 :(得分:5)

这比之前提交的复杂功能要容易得多......

<CFIF FindNoCase('Chrome','#CGI.HTTP_USER_AGENT#') GREATER THAN 0>
  <CFSET browser = 'Chrome'>
</CFIF>

信用:http://www.paulsilver.co.uk/code/coldfusion_detect_browser.php

答案 2 :(得分:4)

有一个用户定义的函数可以解析user_agent字符串。

browserDetect

<cfscript>
/**
 * Detects 130+ browsers.
 * v2 by Daniel Harvey, adds Flock/Chrome and Safari fix.
 * 
 * @param UserAgent      User agent string to parse. Defaults to cgi.http_user_agent. (Optional)
 * @return Returns a string. 
 * @author John Bartlett (jbartlett@strangejourney.net) 
 * @version 4, June 28, 2009 
 */
function browserDetect() {

// Default User Agent to the CGI browser string
var UserAgent=CGI.HTTP_USER_AGENT;

// Regex to parse out version numbers
var VerNo="/?v?_? ?v?[\(?]?([A-Z0-9]*\.){0,9}[A-Z0-9\-.]*(?=[^A-Z0-9])";

// List of browser names
var BrowserList="";

// Identified browser info
var BrowserName="";
var BrowserVer="";

// Working variables
var Browser="";
var tmp="";
var tmp2="";
var x=0;


// If a value was passed to the function, use it as the User Agent
if (ArrayLen(Arguments) EQ 1) UserAgent=Arguments[1];

// Allow regex to match on EOL and instring
UserAgent=UserAgent & " ";

// Browser List (Allows regex - see BlackBerry for example)
BrowserList="1X|Amaya|Ubuntu APT-HTTP|AmigaVoyager|Android|Arachne|Amiga-AWeb|Arora|Bison|Bluefish|Browsex|Camino|Check&Get|Chimera|Chrome|Contiki|cURL|Democracy|" &
            "Dillo|DocZilla|edbrowse|ELinks|Emacs-W3|Epiphany|Galeon|Minefield|Firebird|Phoenix|Flock|IceApe|IceWeasel|IceCat|Gnuzilla|" &
            "Google|Google-Sitemaps|HTTPClient|HP Web PrintSmart|IBrowse|iCab|ICE Browser|Kazehakase|KKman|K-Meleon|Konqueror|Links|Lobo|Lynx|Mosaic|SeaMonkey|" &
            "muCommander|NetPositive|Navigator|NetSurf|OmniWeb|Acorn Browse|Oregano|Prism|retawq|Shiira Safari|Shiretoko|Sleipnir|Songbird|Strata|Sylera|" &
            "ThunderBrowse|W3CLineMode|WebCapture|WebTV|w3m|Wget|Xenu_Link_Sleuth|Oregano|xChaos_Arachne|WDG_Validator|W3C_Validator|" &
            "Jigsaw|PLAYSTATION 3|PlayStation Portable|IPD|" &
            "AvantGo|DoCoMo|UP.Browser|Vodafone|J-PHONE|PDXGW|ASTEL|EudoraWeb|Minimo|PLink|NetFront|Xiino|";
            // Mobile strings
            BrowserList=BrowserList & "iPhone|Vodafone|J-PHONE|DDIPocket|EudoraWeb|Minimo|PLink|Plucker|NetFront|PIE|Xiino|" &
            "Opera Mini|IEMobile|portalmmm|OpVer|MobileExplorer|Blazer|MobileExplorer|Opera Mobi|BlackBerry\d*[A-Za-z]?|" &
            "PPC|PalmOS|Smartphone|Netscape|Opera|Safari|Firefox|MSIE|HP iPAQ|LGE|MOT-[A-Z0-9\-]*|Nokia|";

            // No browser version given
            BrowserList=BrowserList & "AlphaServer|Charon|Fetch|Hv3|IIgs|Mothra|Netmath|OffByOne|pango-text|Avant Browser|midori|Smart Bro|Swiftfox";

            // Identify browser and version
Browser=REMatchNoCase("(#BrowserList#)/?#VerNo#",UserAgent);

if (ArrayLen(Browser) GT 0) {

    if (ArrayLen(Browser) GT 1) {

        // If multiple browsers detected, delete the common "spoofed" browsers
        if (Browser[1] EQ "MSIE 6.0" AND Browser[2] EQ "MSIE 7.0") ArrayDeleteAt(Browser,1);
        if (Browser[1] EQ "MSIE 7.0" AND Browser[2] EQ "MSIE 6.0") ArrayDeleteAt(Browser,2);
        tmp2=Browser[ArrayLen(Browser)];

        for (x=ArrayLen(Browser); x GTE 1; x=x-1) {
            tmp=Rematchnocase("[A-Za-z0-9.]*",Browser[x]);
            if (ListFindNoCase("Navigator,Netscape,Opera,Safari,Firefox,MSIE,PalmOS,PPC",tmp[1]) GT 0) ArrayDeleteAt(Browser,x);
        }

        if (ArrayLen(Browser) EQ 0) Browser[1]=tmp2;
    }

    // Seperate out browser name and version number
    tmp=Rematchnocase("[A-Za-z0-9. _\-&]*",Browser[1]);

    Browser=tmp[1];

    if (ArrayLen(tmp) EQ 2) BrowserVer=tmp[2];

    // Handle "Version" in browser string
    tmp=REMatchNoCase("Version/?#VerNo#",UserAgent);

    if (ArrayLen(tmp) EQ 1) {
        tmp=Rematchnocase("[A-Za-z0-9.]*",tmp[1]);
        BrowserVer=tmp[2];
    }

    // Handle multiple BlackBerry browser strings
    if (Left(Browser,10) EQ "BlackBerry") Browser="BlackBerry";

    // Return result
    return Browser & " " & BrowserVer;

}

// Unable to identify browser
return "Unknown";

}
</cfscript>

答案 3 :(得分:2)

我创建了由BrowscapCFC提供支持的http://browscap.org/。它可能符合您的需求

答案 4 :(得分:1)

ColdFusion中没有内容可以让你这样做。捕获user_agent字符串并手动解析它是您可以做的最好的事情。

第三方应用程序,比如Browserhawk(http://www.cyscape.com/Default.aspx)就可以做到这一点......虽然我自己没有使用过Browserhawk,但我明白它做得很好。

谷歌搜索也出现http://www.andrewdixon.co.uk/aebrowser/,但那已经是6岁了。可能还在工作......但是根据你需要解决方案的防弹程度,你可能想要考虑像Browserhawk这样的东西。您可以谷歌搜索用户代理字符串列表并自行处理...但考虑到跨操作系统的浏览器(和版本)的数量,我认为这本身就是一份全职工作。

答案 5 :(得分:0)

上面的用户定义函数效果很好,但我必须添加以下代码才能使版本号与IE(MSIE)正常工作:

// Handle MSIE Version issue
IF (Left(Browser,4) EQ "MSIE") {
        tmpBrowser = Browser;
    IF (listLen("#tmpBrowser#", " ") EQ 2) {
        Browser = listGetAt("#tmpBrowser#", 1, " ");
        BrowserVer = listGetAt("#tmpBrowser#", 2, " ");
    }
}

使用这样的user_agent:

Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.1; Trident / 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4 .0C)

它返回一个空白版本和浏览器名称“MSIE 8.0”

答案 6 :(得分:0)

这是浏览器名称和版本的最新版本 在Opra / IE 8 / IE 9 / IE 10 / IE 11 / Edge / Chrome / Firefox上测试

<cffunction name="GetBrowser" access="public">
    <cfset var l = {}>
    <cfset l.UserInfo = cgi.HTTP_USER_AGENT>
    <cfset l.Browser = {}>

    <cfif FindNoCase ( "Opera", l.UserInfo ) >
        <cfset l.Browser.type = "Opera">
        <cfset l.Browser.Name = "Opera">
    <cfelseif FindNoCase ( "OPR", l.UserInfo )>
        <cfset l.Browser.type = "OPR">
        <cfset l.Browser.Name = "Opera">
    <cfelseif FindNoCase ( "Edge", l.UserInfo )>
        <cfset l.Browser.type = "Edge">
        <cfset l.Browser.Name = "Edge">
    <cfelseif FindNoCase ( "Chrome", l.UserInfo )>
        <cfset l.Browser.type = "Chrome">
        <cfset l.Browser.Name = "Chrome">
    <cfelseif FindNoCase ( "Safari", l.UserInfo )>
        <cfset l.Browser.type = "Safari">
        <cfset l.Browser.Name = "Safari">
    <cfelseif FindNoCase ( "FireFox", l.UserInfo )>
        <cfset l.Browser.type = "FireFox">
        <cfset l.Browser.Name = "FireFox">
    <cfelseif FindNoCase ( "MSIE", l.UserInfo ) >
        <cfset l.Browser.type = "MSIE">
        <cfset l.Browser.Name = "Internet Explorer">
    <cfelseif FindNoCase ( "Trident", l.UserInfo ) >
        <cfset l.Browser.type = "Trident">
        <cfset l.Browser.Name = "Internet Explorer">
    <cfelse>
        <cfset l.Browser.type = "Unknown">
        <cfset l.Browser.Name = "Unknown">
        <cfset l.Browser.Version = "Unknown">
    </cfif>

    <cfif FindNoCase ( "rv:" , l.UserInfo ) NEQ 0>
        <cfset l.Params = REFindNoCase ( "rv:[0-9.]+", l.UserInfo, Find ( "rv:", l.UserInfo ), true )>
        <cfset l.FirstOccurrence = l.Params.POS[1]>
        <cfset l.ParamLength = l.Params.LEN[1]>
        <cfset l.BrowserNameLength = Len ( "rv:" )>
        <cfset l.Browser.Version = Mid ( l.UserInfo, ( l.FirstOccurrence + l.BrowserNameLength ), ( l.ParamLength - l.BrowserNameLength ) )>
     <cfelse>
        <cfset l.Params = REFindNoCase ( #l.Browser.type# & "/?[ 0-9.]+", l.UserInfo, Find ( "#l.Browser.type#", l.UserInfo ), true )>
        <cfset l.FirstOccurrence = l.Params.POS[1]>
        <cfset l.ParamLength = l.Params.LEN[1]>
        <cfset l.BrowserNameLength = Len ( l.Browser.type ) + 1>
        <cfset l.Browser.Version = Mid ( l.UserInfo, ( l.FirstOccurrence + l.BrowserNameLength ), ( l.ParamLength - l.BrowserNameLength ) )>
     </cfif>

     <cfreturn l.Browser>
</cffunction>

<cfdump var="#GetBrowser()#">

答案 7 :(得分:-1)

此处重构了cfscript版本(仅用于浏览器名称)

<div class="se">
  <div> <img class="seimg" src="images/we.jpg" alt=""></div>
  <div class="sep">
    <h2> Who we are?</h2>
    <p>We have been operating for over 30 years and are Members of The Federation of Master Builders. We work on projects big and small from small residential extensions to full house. We are so happy with this theme. Everyday it make our lives better.
    </p>
    <span>We care about environment.</span>
    <span>We are trusted by hundreds of clients.</span>
    <span>Social media loves us!</span>
    <span>This list is super easy to create</span>

  </div>
</div>