Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need to know when a user is accessing a script with NS4, so that I can drop CSS support. The problem is that the various flavours of NS4 seem to come with very different identifiers as retrieved by $ENV{'HTTP_USER_AGENT') that are not trappable with a generic RegExp (or at least not without excluding CSS complient browsers).

Does anyone know of any resources, etc. that would let me identify the browser and version? Most of the stuff I've managed to track down seems very out of date.

Replies are listed 'Best First'.
Re: Identifying browsers
by Corion (Patriarch) on Mar 30, 2001 at 17:51 UTC

    A short search with Google brings me to this page, which tells me that most Netscape strings start with something like Mozilla/x.xxx(, and after that first opening parens, there is not the word compatible. This could be checked for with the following crummy code :

    sub header_NS { # Assume that nobody uses Netscape anymore my $result = undef; # Get user agent passed as the only parameter my $user_agent = shift; if ($user_agent =~ m#^Mozilla/\d+#i) { # We have a Mozilla/NetScape compatible browser if ($user_agent !~ /compatible/i) { # And it's not even just "compatible", it's the real, # CSS burning, X crashing hellspawn $result = 1; }; }; return $result; };

    The above code checks for any browser that claims to beo Mozilla, but also claims to be the real thing and not something compatible. But I think your idea of using User-Agent headers for this is flawed, as there are many instances where proxies change the user agent and there are also other browsers which have real problems with CSS support. I recommend you put a "This site without CSS" link into your script, which calls your script again with the parameter ?nocss=1 appended to the URL. This allows users with broken browsers to easily use your website even if your magic detection does not work because you didn't anticipate their specific browser.

      your code will find all versions of Netscape, including fully CSS-compatible ones. for people looking for this solution in the NS6+/Mozilla era, add
      if ($user_agent !~ /gecko/i) {
      right before the
      $result = 1
      and match brackets as appropriate.

      I really hate it when a website decides my Mozilla 1.1b is not capable of rendering basic CSS (some sites won't even let me in).
      Thanks, good advice (I'd already planned to allow the option of disabling CSS, but it's nice to have it confirmed as a good idea)
      Tom Melly, tom@tomandlu.co.uk