Server side browser detection (Useful tip)

You can do a great deal of browser portability handling if you are prepared to serve browser specific pages from your web server.

A wholly dynamic site may be able to serve browser, platform and version specific HTML according to the value of the userAgent string that the browser sends when it makes a request. This technique is fine for a few pages and when they don't experience high traffic. To serve static pages this way needs a smarter server-side trick to be deployed.

You could render very browser specific copies of your pages and store them under document path names that contain a component that could be derived by disassembling the userAgent string. Then in your web server, you can trap every request that needs this capability, route it through a special module and generate a browser-specific path modification. If you do this creatively, you could provide a mechanism that allows the web pages to request an unmodified URL but the web server serves a page that is as close to ideal as it can get.

For example, the browser might be Netscape 3 on a Macintosh. That might yield a string containing a 3 for the version, an N for the browser and an M for the platform. The 3 might become a 4 or even a 6 for other versions of Netscape Navigator. The N might become an E for MSIE, an I for iCab or an O for Opera. The M might become a W for windows. So we have a string that represents the browser, platform and version in three letters. We might get a string such as NM3, for example. When the browser requests a page called index.html, the web server would inspect the userAgent string and work out that its normalized signature is NM3. The web server can then attempt to serve the page NM3index.html. If this does not exist, the web server can fall back to NMindex.html and then Nindex.html before eventually serving just plain old index.html. These tests in the web server will take fractions of a second with something like a stat() function call to test for the existence of a file.

With this technique, your publishing logic can generate some very platform, browser and version specific static files and the web server can locate them quickly and efficiently even when there are high traffic loads on the server farm. It's also workable for style sheets and can be deployed in a load-balanced multi-machine server farm as well.

See also:Compatibility