"How to fix `remove default alphabetical ordering of SerializeJSON() ` - coldfusion

I'm trying to add the serialized data in a request to third party API which needs a specific order of the data to be maintained, but SerializeJSON orders in alphabetical order which breaks the format required by the third party API. Could someone help me to figure it out
INPUT:
<cfset data ={
"Booking": {
"ActionCode":"DI",
"AgencyNumber":"23",
"Touroperator":"TVR",
"BookingNumber":"323",
},
"Payment": {
"__type":"paymenttype",
"PaymentProfile": {
"Value": 4,
"Manual": false
},
"PaymentType": 4,
"PaymentAction":2,
"Details": {
"IBAN": "DE02120300000000202051",
"BIC": "BYLADEM1001"
}
},
"Login":{
"UserCode": "usercode",
"Password": "password"
}
}>
When this method SerializeJSON() is used on my data:
SerializeJSON(data)
Current Output
"{"Booking":{"Touroperator":"TVR","ActionCode":"DI","BookingNumber":"323","AgencyNumber":"23"},"Login":{"UserCode":"usercode","Password":"password"},"Payment":{"PaymentProfile":{"Manual":false,"Value":4},"PaymentType":4,"PaymentAction":2,"__type":"paymenttype","Details":{"BIC":"BYLADEM1001","IBAN":"DE02120300000000202051"}}}"
Expected Output:
"{"Booking":{"ActionCode":"DI","AgencyNumber":"23","Touroperator":"TVR","BookingNumber":"323",},"Payment":{"__type":"paymenttype","PaymentProfile":{"Value":4,"Manual":false},"PaymentType":4,"PaymentAction":2,"Details":{"IBAN":"DE02120300000000202051","BIC":"BYLADEM1001"}},"Login":{"UserCode":"usercode","Password":"password"}}"

Structs in ColdFusion are unordered HashMaps, so there is no order at all. You can keep insertion order by using structNew("Ordered") (introduced with ColdFusion 2016). Unfortunately you can no longer use the literal syntax anymore, but I assume you are generating the data dynamically anyway.
<cfset data = structNew("Ordered")>
<cfset data["Booking"] = structNew("Ordered")>
<cfset data["Booking"]["ActionCode"] = "DI">
<cfset data["Booking"]["AgencyNumber"] = "TVR">
<cfset data["Booking"]["BookingNumber"] = "323">
<cfset data["Payment"] = structNew("Ordered")>
<cfset data["Payment"]["__type"] = "paymenttype">
<cfset data["Payment"]["PaymentProfile"] = structNew("Ordered")>
<cfset data["Payment"]["PaymentProfile"]["Value"] = 4>
<cfset data["Payment"]["PaymentProfile"]["Manual"] = false>
etc.
If you are stuck on an older ColdFusion version, you will have to use Java's LinkedHashMap.
<cfset data = createObject("java", "java.util.LinkedHashMap")>
<cfset data["Booking"] = createObject("java", "java.util.LinkedHashMap")>
<cfset data["Booking"]["ActionCode"] = "DI">
<cfset data["Booking"]["AgencyNumber"] = "TVR">
<cfset data["Booking"]["BookingNumber"] = "323">
<cfset data["Payment"] = createObject("java", "java.util.LinkedHashMap")>
<cfset data["Payment"]["__type"] = "paymenttype">
<cfset data["Payment"]["PaymentProfile"] = createObject("java", "java.util.LinkedHashMap")>
<cfset data["Payment"]["PaymentProfile"]["Value"] = 4>
<cfset data["Payment"]["PaymentProfile"]["Manual"] = false>
etc.
But be aware: LinkedHashMap is case-sensitive (and also type-sensitive: in case your keys are numbers, it does matter!).
<cfset data = createObject("java", "java.util.LinkedHashMap")>
<cfset data["Test"] = "">
<!---
accessing data["Test"] = works
accessing data["test"] = doesn't work
accessing data.Test = doesn't work
--->
Another issue you might encounter: Due to ColdFusion's internal type casting, serializeJSON() might stringify numbers and booleans in an unintended way. Something like:
<cfset data = structNew("Ordered")>
<cfset data["myBoolean"] = true>
<cfset data["myInteger"] = 123>
could easily end up like:
{
"myBoolean": "YES",
"myInteger": 123.0
}
(Note: The above literal syntax would work perefectly fine, but if you are passing the values around as variables/arguments, casting eventually happens.)
The easiest workaround is explicitly casting the value before serializing:
<cfset data = structNew("Ordered")>
<cfset data["myBoolean"] = javaCast("boolean", true)>
<cfset data["myInteger"] = javaCast("int", 123)>

Related

coldfusion 11 loop in rightful way

i see this code in one of the pages, seems annoying, it can be shorted, if i am on Coldfusion 11
https://trycf.com/gist/701515844d5e41549a7b6e61dfafeaa0/lucee5?theme=monokai
Here is the code how its looks like
<cfloop list="#mqry.columnList#" index="col">
<cfset ColRow ="#mqry.columnList#," >
</cfloop>
<cfset x = replace(ColRow,"CONDITION1","CONDITION") & chr(13) & chr(10)>
<cfloop query="mqry">
<cfloop list="#mqry.columnList#" index="colN">
<cfset f = Replace("#mqry[colN][currentRow]#" ,"""","","All")>
<cfset f = REReplaceNoCase("#mqry[colN][currentRow]#" , "<[^>]*(?:>|$)", "", "ALL")>
<cfset x = x & """#_f#"",">
</cfloop>
<cfset x = x & chr(13) & chr(10)>
</cfloop>
the first value i think i can do as:
mquery.columnlist - no need to loop over
There's nothing wrong with the nested loop. You iterate over all rows and then iterate over each cell per row using the column name. I rewrote your code to be easier understandable:
<cfset NEWLINE = (chr(13) & chr(10))>
<cfset DELIM = ",">
<cfset QUOTE = '"'>
<!--- column headers --->
<cfset x = mqry.columnList>
<!--- replace column names as desired --->
<cfset x = replace(x, "CONDITION1", "CONDITION")>
<!--- next line --->
<cfset x &= NEWLINE>
<cfloop query="mqry">
<cfset row = []>
<cfloop list="#mqry.columnList#" index="colN">
<cfset cellValue = mqry[colN][mqry.currentRow]>
<!--- remove all quotes --->
<cfset cellValue = replace(cellValue, QUOTE, "", "ALL")>
<!--- remove tags --->
<cfset cellValue = reReplaceNoCase(cellValue, "<[^>]*(?:>|$)", "", "ALL")>
<!--- prefix with underscore --->
<cfset cellValue = ("_" & cellValue)>
<!--- wrap in quotes --->
<cfset cellValue = (QUOTE & cellValue & QUOTE)>
<cfset row.add(cellValue)>
</cfloop>
<!--- combine cell values into a row --->
<cfset x &= arrayToList(row, DELIM)>
<!--- next line --->
<cfset x &= NEWLINE>
</cfloop>
<cfoutput>#x#</cfoutput>

using List to delete elements from middle, first position and last position in sequence

trying to handle the list deletion but stuck on ts deletion process: here is my small gist which i am working on, any help appreciated
https://trycf.com/gist/aa3871e76db36ad446b770b9bbbb4cec/lucee5?theme=monokai
<cfset lstFirst = "1,2,3,4,5">
<cfset lstMiddle = "6,7,8,9,10">
<cfset lstLast = "11,12,13,14,15">
From the lstFirst I should be able to delete from reverse like 5,4,3,2,1 but i should not be able to
delete from middle of the very next item which is 2
From the lstMiddle if the middle one is 8, i should be able to delete 7,6 and so on or 9,10 but i should not be able to
delete 6,10 without deleting 7 and 9
From the lstLast which is complete reverse of the lstFirst where the last element is selected and i should be able to delete
from 14,13,12 onward but cannot delete from middle or from 11 onward, it must start its deletion from 14 or 15 backwards this is the way i am trying to make it work
**<cfoutput>
<cfset lstFirst = "1,2,3,4,5">
<cfset lstMiddle = "6,7,8,9,10">
<cfset lstLast = "11,12,13,14,15">
<!--- It's work all dynamic values for lstFirst like 1,2,3,4,5 OR 1,2,3,4,5,6,7,8,9,10 OR 1,2,3,4,5,6,7,8,9,10,11,12 etc.... --->
======= Delete all number from reverse and except very next item to middle ====
<cfset convertToArray = listtoarray(lstFirst)>
<cfset getRoundValue =round(arraylen(convertToArray)/2)>
<cfset findMidNextEle = ArraySlice(convertToArray,1,getRoundValue)>
<cfset LfirstMiddle = listgetat(lstFirst,listfind(lstFirst,listlast(arraytolist(findMidNextEle)))-1)>
<cfloop from="#listlen(lstFirst)#" to="1" index="i" step="-1">
<cfset listgetVal = listgetat(lstFirst,i)>
<cfif LfirstMiddle NEQ listgetVal >
<cfset lstFirst =listdeleteat(lstFirst,i)>
</cfif>
<br/><cfdump var="#lstFirst#" /><br/>
</cfloop>
=========== Delete 7,6 and so on or 9,10 and delete 6,10 without deleting 7 and 9 ================================= <br/>
<cfset lstMiddleMiddle = listfind(lstMiddle,listgetat(lstMiddle,listfind(lstMiddle,listlast(mid(lstMiddle,1,listlen(lstMiddle)))))) >
<cfloop from="1" to="#listlen(lstMiddle)#" index="j">
<cfset userList = listlen(lstMiddle)>
<cfif userList GT 3 >
<cfset lstMiddle1 = listdeleteat(lstMiddle,lstMiddleMiddle-1)>
<cfset lstMiddle2 = listdeleteat(lstMiddle1,lstMiddleMiddle)>
<cfset lstMiddleMiddle = listfind(lstMiddle2,listgetat(lstMiddle2,listfind(lstMiddle2,listlast(mid(lstMiddle2,1,listlen(lstMiddle2))))))>
<cfset lstMiddle = lstMiddle2>
<br/><cfdump var="#lstMiddle2#" /></br>
<cfelse>
<cfset lstMiddle3 = listdeleteat(lstMiddle2,1)>
<cfset lstMiddle4 = listdeleteat(lstMiddle3,2)>
<cfbreak/>
</cfif>
</cfloop>
enter code here
<cfdump var="#lstMiddle4#" /> <br/>
=============== Delete from reverse except last number ==========
<!--- It's will work for all dynamic values --->
<cfset LastEle = listlast(lstLast)>
<cfloop from="#listlen(lstLast)#" to="1" step="-1" index="k">
<cfset getCurrentValue = listgetat(lstLast,k)>
<cfif LastEle NEQ getCurrentValue>
<cfset lstLast =listdeleteat(lstLast,k)>
</cfif>
</br><cfdump var="#lstLast#" /><br/>
</cfloop>
</cfoutput>**

How to output the values of a query depending on the value of row?

If I have a query result like this
type_name | count
-----------------
test1 | 5
test2 | 4
test4 | 7
How to output the count base on the value of the column type_name?
From the table the row value 'test3' doesn't exist, but it doesn't mean it wont exist later after a refresh.
With the below code I will only get the value but looping 3 times since test3 value doesn't exist.
<cfoutput name="query">
<table>
<tr><td><cfif query.type_name eq 'test1'>#query.count#</cfif></td></tr>
<tr><td><cfif query.type_name eq 'test2'>#query.count#</cfif></td></tr>
<tr><td><cfif query.type_name eq 'test3'>#query.count#</cfif></td></tr>
<tr><td><cfif query.type_name eq 'test4'>#query.count#</cfif></td></tr>
</cfoutput>
<cfset testResults = [0,0,0,0]>
<cfloop query="query">
<cfset testResults[right(query.type_name, 1)] = query.count>
</cfloop>
<table>
<cfloop array="#testResults#" index="count">
<td>#count#</td>
</cfloop>
</table>
You should see
5
4
0
7
If you need a more dynamic approach, you can transform your full query result (all columns) to an array of structs:
<!---
<!--- test data --->
<cfset query = queryNew("type_name,count", "VARCHAR,INTEGER")>
<cfset queryAddRow(query, 3)>
<cfset querySetCell(query, "type_name", "test1", 1)>
<cfset querySetCell(query, "count", "5", 1)>
<cfset querySetCell(query, "type_name", "test2", 2)>
<cfset querySetCell(query, "count", "4", 2)>
<cfset querySetCell(query, "type_name", "test4", 3)>
<cfset querySetCell(query, "count", "7", 3)>
--->
<!--- number of tests to list --->
<cfset expectedNumberOfRows = 4>
<!--- remember all columns while transforming query to array of structs --->
<cfset queryColumns = listToArray(query.columnList)>
<!--- initialize array of structs --->
<cfset testResults = arrayNew(1)>
<cfloop from="1" to="#expectedNumberOfRows#" index="i">
<cfset blankResult = structNew()>
<!--- initialize all columns for each row --->
<cfloop array="#queryColumns#" index="columnName">
<cfset blankResult[columnName] = "">
</cfloop>
<!--- default values for specific columns --->
<cfset blankResult["type_name"] = "test#i#">
<cfset blankResult["count"] = "0">
<cfset testResults.add(blankResult)>
</cfloop>
<!--- transfer cell values from each query row to array of structs --->
<cfset queryRowIndex = 1>
<cfloop query="query">
<!--- extract possible row index --->
<cfset testNumber = trim( reReplace(query.type_name, "[^0-9]*([0-9]+)$", "\1") )>
<!--- transfer cells --->
<cfif reFind("^[0-9]+$", testNumber)>
<cfloop array="#queryColumns#" index="columnName">
<cfset testResults[int(testNumber)][columnName] = query[columnName][queryRowIndex]>
</cfloop>
</cfif>
<cfset queryRowIndex++>
</cfloop>
<cfoutput>
<table>
<cfloop array="#testResults#" index="testResult">
<tr>
<td>#testResult.type_name#</td>
<td>#testResult.count#</td>
<!--- add additional columns if desired --->
</tr>
</cfloop>
</table>
</cfoutput>

CFML Efficiently determine if a value exists in one of multiple lists

This is my first post to SO, a resource that is incredibly valuable!
I am trying to determine if a value (state code) exists in a list of codes and if so, set a new variable that represents the division (ie. sales territory, no match = 15). The code below works, but I want to improve my skills and do this as efficiently as possible. So my question is there a "better" way to achieve the same result?
<cfset state = "LA">
<cfset division = 15>
<cfset position = 0>
<cfset aStates = ArrayNew(1)>
<cfset aStates[1] = "MA,ME,NH,RI,VT">
<cfset aStates[2] = "CT,DE,NJ,NY,DE">
<cfset aStates[3] = "DC,MD,VA,WV">
<cfset aStates[4] = "TN">
<cfset aStates[5] = "NC,SC">
<cfset aStates[6] = "GA">
<cfset aStates[7] = "FL">
<cfset aStates[8] = "AL,KY,LA,MS">
<cfset aStates[9] = "IL,WI">
<cfset aStates[10] = "CO,MN,ND,SD,WY">
<cfset aStates[11] = "IN,OH,MI">
<cfset aStates[12] = "ID,OR,UT,WA">
<cfset aStates[13] = "AZ,HI,NV">
<cfset aStates[14] = "CA">
<cfset position = 0>
<cfloop array="#aStates#" index="lStates">
<cfset position = position+1>
<cfif ListFindNoCase(lStates,variables.state) NEQ 0>
<cfset division = position>
</cfif>
</cfloop>
<cfdump var="#aStates#" label="states array">
<cfoutput>State: #state#</cfoutput>
<cfoutput>Division: #division#</cfoutput>
With the current structure, you are relying on string comparisons, so there is not too much room for improvement.
Unless there is a specific reason you must hard code the values, a more flexible approach is to store the information the database. Create tables to store the "states" and "divisions", and another table to store the relationships:
States
StateID | Code | Name
1 | ME | Maine
2 | GA | Georgia
3 | CA | California
4 | NH | New Hampshire
...
Divisions
DivisionID | Name
1 | Sales Territory
...
DivisionStates
DivisionID | StateID
1 | 1
1 | 4
6 | 2
14 | 3
...
Then use an OUTER JOIN to retrieve the selected state and division. Assuming #state# will always contain a valid entry, something along these lines (not tested):
SELECT s.Name AS StateName
, CASE WHEN d.Name IS NULL THEN 'No Match' ELSE d.Name END AS DivisionName
FROM States s
LEFT JOIN DivisionStates ds ON ds.StateID = s.StateID
LEFT JOIN Divisions d ON d.DivisionID = ds.DivisionID
WHERE s.Code = <cfqueryparam value="#state#" cfsqltype="cf_sql_varchar">
I do not know the source of the #state# variable, but I am guessing it may be passed via a form <select>. If it is currently hard coded, you could simplify things further by using querying the "states" table, and using it to populate the list. Also, consider using the ID as the list value, rather than the state "code".
I thought of an approach in this by placing the states into a Struct; with the state codes as keys.
<cfscript>
state = "LA";
division = 15;
states_divisions_map = {
MA: 1, ME: 1, NH: 1, RI: 1, VT: 1,
CT: 2, NJ: 2, NY: 2, DE: 2,
DC: 3, MD: 3, VA: 3, WV: 3,
TN: 4,
NC: 5, SC: 5,
GA: 6,
FL: 7,
AL: 8, KY: 8, LA: 8, MS: 8,
IL: 9, WI: 9,
CO: 10, MN: 10, ND: 10, SD: 10, WY: 10,
IN: 11, OH: 11, MI: 11,
ID: 12, OR: 12, UT: 12, WA: 12,
AZ: 13, HI: 13, NV: 13,
CA: 14
};
if(StructKeyExists (states_divisions_map, state) ){
division = states_divisions_map[state];
}
writeDump(var: states_divisions_map, label: "States");
writeOutput("State: #state#");
writeOutput("Division: #division#");
</cfscript>
This way you can quickly check for the state without all the loops.
Observation: In your original code, DE is present twice.
ListContains() is like ListFind(), but it searches for a list element that contains the the text you're looking for anywhere in it. (ListFind() looks for a list element that entirely matches the string. Generally ListFind() is the better tool but not in this case.
(CF does have an ArrayContains, but it doesn't work for this task, it merely tells you if the array contains an exactly matched element.)
Because you're searching for unique two-char state-codes, this is a perfect use of the function.
<cfset aStates = ArrayNew(1)>
<cfset aStates[1] = "MA,ME,NH,RI,VT">
<cfset aStates[2] = "CT,DE,NJ,NY,DE">
<cfset aStates[3] = "DC,MD,VA,WV">
<cfset aStates[4] = "TN">
<cfset aStates[5] = "NC,SC">
<cfset aStates[6] = "GA">
<cfset aStates[7] = "FL">
<cfset aStates[8] = "AL,KY,LA,MS">
<cfset aStates[9] = "IL,WI">
<cfset aStates[10] = "CO,MN,ND,SD,WY">
<cfset aStates[11] = "IN,OH,MI">
<cfset aStates[12] = "ID,OR,UT,WA">
<cfset aStates[13] = "AZ,HI,NV">
<cfset aStates[14] = "CA">
<cfset lstates = arraytolist(astates,"|")>
<cfset division = listcontainsnocase(lstates,"CO","|")>
<cfoutput>Division: #division#<br>
State: #state#</cfoutput>
We set the delimiter to | (pipe), but you can use anything, other than the default (comma), because you're using that for you're sub-delimiter.
For future reference, if you're using CF 9 (I believe) or after, you can use array shorthand for quickly building arrays.
<cfset aStates=["MA,ME,NH,RI,VT","CT,DE,NJ,NV,DE","DC,MD,VA,WV"]> ...
While it may seem merely like a stylistic difference, it saves a lot of time.
Structures can be created similarly.
<cfset MyDogIs = {Size = "medium", Color = "Black", HasPaws = ["FL","FR","BL","BR"]}>
(And you can nest implicit array and structs within another!)
Thank you all for your input. I am answering this question myself with the code I decided to use since it is based on piece supplied by multiple answers/comments. I hope this is the correct way to do this, if not please advise.
The divisions were not stored in the db as they are a set it and forget it mapping that has not changed in years and I thought I'd save a call to the DB. If they were subject to change I would have taken a table approach suggested by #leigh
Thanks to #matt-busche for the improved loop, ucase() and break recommendations, and #cfqueryparam for the short hand array suggestion.
Here's what I went with. The code is actually a function in a cfc that processes a form submission but I have pasted only the relevant part.
<cfset form.division = 15>
<cfset aDivisions = ["MA,ME,NH,RI,VT",
"CT,DE,NJ,NY,DE",
"DC,MD,VA,WV",
"TN",
"NC,SC",
"GA",
"FL",
"AL,KY,LA,MS",
"IL,WI",
"CO,MN,ND,SD,WY",
"IN,OH,MI",
"ID,OR,UT,WA",
"AZ,HI,NV",
"CA"]>
<cfloop from="1" to="#ArrayLen(aDivisions)#" index="i">
<cfif ListFind(aDivisions[i],Ucase(arguments.sForm.state)) NEQ 0>
<cfset form.division = i>
<cfbreak>
</cfif>
</cfloop>

Obtaining browser and version using Coldfusion

Is there something built into Coldfusion that would let me know the browser and version number surfing my site? Are there existant libraries that would enable me to do this?
My google-fu hasn't shown many things other than simple if statements detecting IE.
I did do a dump of the cgi.http_user_agent variable in Chrome:
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.77 Safari/534.24
There's three different browsers mentionned in there! Doing an if statement looking for either would return true. There's got to be a more formal approach?
This is much easier than the complicated function submitted earlier...
<CFIF FindNoCase('Chrome','#CGI.HTTP_USER_AGENT#') GREATER THAN 0>
<CFSET browser = 'Chrome'>
</CFIF>
credit: http://www.paulsilver.co.uk/code/coldfusion_detect_browser.php
Could you not use javascript instead? http://cssuseragent.org/
CSSUA is awesome, adds simple classes to the tag and allows you to target content in CSS/Javascript using classes like ua-ie-6 etc
There is a User-Defined Function which can parse out the user_agent string.
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>
I created BrowscapCFC powered by http://browscap.org/. It may fit your needs
There's nothing built into ColdFusion that will allow you to do this. Capturing the user_agent string and parsing it manually is about the best you can do.
There are third party apps like Browserhawk (http://www.cyscape.com/Default.aspx) that will do this... and while I've not used Browserhawk myself, I understand that it does the job well.
Googling also turns up http://www.andrewdixon.co.uk/aebrowser/, but that is 6 years old. Might still work... but depending on how bulletproof you need the solution to be, you might want to consider something like Browserhawk. You could google for a list of user agent strings and handle it yourself... but given the sheer number of browsers (and versions) across OS's, I think that'd be a full time job in and of itself.
The user defined function above worked great, but I had to add the following code to get the version number to work correctly with 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, " ");
}
}
With a user_agent like this:
Mozilla/4.0 (compatible; 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)
It was returning a blank version and a browser name of "MSIE 8.0"
Here is a recent build of the browser name and version
Tested on 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()#">
Here is refactored (from above) cfscript version for just browser name
function GetBrowserName() {
var rs = "Unknown";
var uag = cgi.HTTP_USER_AGENT;
if (FindNoCase("Opera",uag))
rs = "Opera";
else if (FindNoCase("OPR",uag))
rs = "Opera";
else if (FindNoCase("Edge",uag))
rs = "Edge";
else if (FindNoCase("Chrome",uag))
rs = "Chrome";
else if (FindNoCase("Safari",uag))
rs = "Safari";
else if (FindNoCase("Firefox",uag))
rs = "Firefox";
else if (FindNoCase("MSIE",uag))
rs = "IExplore";
else if (FindNoCase("Trident",uag))
rs = "IExplore";
else
rs = "Unknown";
return rs;
}