1 Reply Latest reply on Sep 5, 2013 6:33 AM by 'G'

    Getting browser / version from cgi.http_user_agent

    'G' Level 1


      I am succesfully storing away into the database each individuals cgi.http_user_agent.


      What a want to do is chop out the browser/platform and version number from the string and display it in a cfgraph for stat puposes.  Is there any easy way of doing this?


      Does somebody have a function I can look at/use?


      Many thanks,


        • 1. Re: Getting browser / version from cgi.http_user_agent
          'G' Level 1

          Hi All,


          I have managed to find a function that strips out the irrelecant information from the cgi.http_user_agent and displays the information like so...


          MSIE 9.0

          MSIE 9.0

          MSIE 10.0

          Chrome 28.0.1500.95

          Chrome 28.0.1500.95

          Chrome 29.0.1547.62

          Chrome 29.0.1547.62

          Chrome 29.0.1547.62

          Firefox 19.0

          Firefox 23.0


          Here is my code...


          <cfquery name="get_browserstats" datasource="XXX">

                    SELECT            XXX_NAME

                    FROM           XXX_NAME_TABLE

                    ORDER BY upper(XXX_NAME)




          <cfset myArray = ArrayNew(1)>



          <cfoutput query="get_browserstats">



          * Detects 130+ browsers.

          * v2 by Daniel Harvey, adds Flock/Chrome and Safari fix.        

          * v5 fix by RCamden based on bug found by Jeff Mayer


          * @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 5, October 10, 2011


          function browserDetect() {



              // Default User Agent to the CGI browser string

              var UserAgent=#BROWSER_VER#;


              // 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|Chec k&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_Arac hne|WDG_Validator|W3C_Validator|" &

                          "Jigsaw|PLAYSTATION 3|PlayStation Portable|IPD|" &

                          "AvantGo|DoCoMo|UP.Browser|Vodafone|J-PHONE|PDXGW|ASTEL|EudoraWeb|Minimo|PLink|NetFront|X iino|";

                          // Mobile strings

                          BrowserList=BrowserList & "iPad|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



              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);



                      for (x=ArrayLen(Browser); x GTE 1; x=x-1) {


                          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]);





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


                  // Handle "Version" in browser string


                  if (ArrayLen(tmp) EQ 1) {


                      //hack added by Camden to try better handle weird UAs

                      if(arrayLen(tmp) eq 2) BrowserVer=tmp[2];

                      else browserVer=tmp[1];



                  // Handle multiple BlackBerry browser strings

                  if (Left(Browser,10) EQ "BlackBerry") Browser="BlackBerry";


                  // Return result

                  return Browser & " " & BrowserVer;




              // Unable to identify browser

              return "Unknown";







          <cfset browser_array = #ArrayAppend(myArray, "#browserDetect()#")#>




          <cfset myList = ArrayToList(myArray, "<br>")>








          What I want to do is use this information on my array and place it into a nice <cfchart> pie graph... although I am struggling.... Anyone got any useful tips on how to do this?  It would be much apprciated..


          Many thanks,