Check visitors web browsers in ColdFusion
The code CGI.HTTP_USER_AGENT displays the 'user agent' string that the browser sends to your web server, and you can use this to detect which browser they are using.
The following finds out if the visitor is using the Apple browser Safari and puts that in the variable 'browser':
<CFIF FindNoCase('Safari','#CGI.HTTP_USER_AGENT#') GREATER THAN 0>
<CFSET browser = 'Safari'>
This mess continues until today, with Internet Explorer 6 reporting itself as "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" But at least we can do a search for MSIE within the user agent string to find out that it is, indeed, Internet Explorer after all.
So rather than reporting this: "Opera/7.11 (Windows NT 5.0; U) [en]", it's true user agent.
It can be set to identify itself as MSIE 6: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Opera 7.11 [en]"
Fortunately, it still says it's Opera, just later in the string. If you are looking for something that is Internet Explorer, rather than something else, it can be worth looking for it like this:
<CFIF FindNoCase('MSIE 6','#CGI.HTTP_USER_AGENT#') GREATER THAN 0 AND FindNoCase('Opera','#CGI.HTTP_USER_AGENT#') LESS THAN 1>
<CFSET browser = 'Internet Explorer 6'>
That is, looking for Opera in the string as well, just to make sure it's not Opera fooling you.
When I've needed to do detection for a client to show which browsers were using their search facility, I looked for these:
|In User Agent||Is Browser|
|MSIE 6 (without Opera)||Internet Explorer 6|
|MSIE 5.5 (without Opera)||Internet Explorer 5.5|
|MSIE 5.0 or 5.1 (without Opera)||Internet Explorer 5|
|MSIE 4 (without Opera)||Internet Explorer 4|
|MSIE 3 (without Opera)||Internet Explorer 3|
|Opera 4||Opera 4|
|Opera 5||Opera 5|
|Opera 7||Opera 7|
|Mozilla 4 (without 'compatible')||Netscape 4|
|Gecko (with 'Mozilla', without 'Firebird')||Mozilla|
OK, Mosaic is really only in there for old time's sake (first web browser I, and pretty much anyone else, used) and Lynx and WebTV are very rare, but I've had hits for all of the others.
Paul Silver. December 2003