Picture of Paul Silver

Paul Silver

Making websites for companies and helping advertise them

Check visitors web browsers in ColdFusion

Sometimes it's useful to find out which web browser your visitors are using, for instance for logging, or serving a different javascript to the page.

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'>
</CFIF>

Unfortunately, the user agent is sometimes not what you might expect. It has been polluted over the years, mainly to let old JavaScript code know which standard the browser was using so it could run properly. Originally, finding 'Mozilla' in the user agent meant the browser was Netscape, the first browser to use JavaScript. But, when Internet Explorer started supporting the same JavaScript commands as Netscape, they started putting 'Mozilla' in their user agent as well, so the JavaScript looking for Netscape would be fooled in to running.

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.

Currently, some websites look for Internet Explorer, and if you're not using it do not let you use their website (the UK chain Marks and Spencer was guilty of this until recently.) This is generally due to websites trying to use complicated JavaScript for things like menus, and not being willing to put in the right amount of testing to get them working on all browsers. The browser Opera will emulate other browsers by changing its user agent so websites are fooled in to thinking it's Internet Explorer to get around these problems.

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'>
</CFIF>

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/3 Opera 3
Opera 4 Opera 4
Opera 5 Opera 5
Opera/6 Opera 6
Opera 7 Opera 7
Safari Safari
Netscape6 Netscape 6
Mozilla 4 (without 'compatible') Netscape 4
Gecko (with 'Mozilla', without 'Firebird') Mozilla
Firebird Mozilla Firebird
Konqueror Konqueror
WebTV WebTV
Lynx Lynx
Mosaic Mosaic

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.

More code articles


Paul Silver. December 2003

© 2024 Paul Silver & Silver Web Services Ltd