Re: distinguishing browser types

Royston Shufflebotham (rws@ukc.ac.uk)
Fri, 21 Apr 1995 07:52:31 +0500


Rick> I wonder if someone could point me to some code that would show
Rick> how to serve up data tailored to a particular browser.
Rick> Specifically, I would like to serve a file with some tables as
Rick> <PRE> blocks for browsers that do not get tables yet, and html3
Rick> table stuff for browsers that do.
Rick> Thanks In advance.
Rick> Rick Silterra

I use some code like this in a page on our server.

The bit of code which detects which browser is being used looks like:

------
$user_agent=$ENV{"HTTP_USER_AGENT"};
$html3_table_support=0;

if ($user_agent=~/Mozilla/) { # Netscape
($version)=($user_agent=~m@Mozilla/([\d\.]*)@);
if ($version>=1.1) {
$html3_table_support=1;
}
} elsif ($user_agent=~/Arena/) { # Arena
$html3_table_support=1;
} elsif ($user_agent=~/NCSA Mosaic/) { # Mosaic
($version)=($user_agent=~m@^.*?/([\d\.]*)@);
if ($version>=2.5) {
$html3_table_support=1;
}
}
------

Currently, this only spots Netscape v1.1 or above, Mosaic v2.5 or
above, or any version of Arena..
(btw - you'll need Perl5 for the non-greedy regexps there)

Just before the data gets printed out, I prepare one of two
templates for a printf:

------
$template="%-23s %5s %-15s %-5s %-25s %5s\n";
$template="<TR>
<TD align=left><P>%s</P></TD>
<TD><P>%s</P></TD>
<TD><P>%s</P></TD>
<TD><P>%s</P></TD>
<TD align=left><P>%s</P></TD>
</TR>\n" if $html3_table_support;
------

Then, this section of code prints out the heading line, if necessary:

------
if (!$head) {
if ($html3_table_support) {
print "<TABLE>\n";
print "<TR><TH ALIGN=left><P>Name</P></TH>\n";
print "<TH><P>Phone</P></TH>\n";
print "<TH><P>Last seen when?</P></TH>\n";
print "<TH><P>Room</P></TH>\n";
print "<TH><P>Description</P></TH>\n";
print "</TR>\n";
} else {
print "<PRE>\n";
printf $template, "Name", "Phone", "Last seen when?","Room", "
Description";
print "\n";
}
$head=1;
}
------

To the print the individual rows in the table:

-------
printf $template, ${$info}{"surname"}.", ".${$info}{"forename"},
${$info}{"phone"}, $nicetime, ${$info}{"room"}, $infomsg;
-------

Then, obviously, at the bottom of the table, I close up the <PRE>
or <TABLE> section.

You can see this code in action at
<URL:http://nuntius.ukc.ac.uk/cgi-bin/abinfo>

Hope this is of use,
Royston.

--------------------------------------------------------------
Royston Shufflebotham | Computing Laboratory, The University,
Postgraduate Research | Canterbury, Kent, CT2 7NF, UK
Mobile Computing | Tel.: (+44) [0]1227 764000, Ext.3822
--------------------------------------------------------------