Remote Agent Detection

Last Updated: November 17, 1998

One way to insure that a seamless presentation is given to all clients that visit your web pages is to switch on the agent type (browser) and issue different HTML source (e.g. non-enhanced versus enhanced) to the client. A simple CGI and HTML file can be designed to facilitate such a task. The HTML file and CGI source are presented in the sections below.

  1. The source code for remote agent detection
  2. Installation instructions


The Source Code for Remote Agent Detection

The source code that handles the remote agent detection has been released to the public domain. The browser configuration section of the source code should be changed to associate a URL with each browser (see instructions included in the source code).


#!/usr/bin/perl
#
# agent.pl  
#
# simple redirection script, based on browser 
# (see configuration section below)
# 

############################################################
#
# Browser Configuration
#
# browser/URL pairs- "unique browser key", "URL"
# the following is an associative array which is composed of
# browsers in the "left" slots and URL's relative to your home
# directory in the "right" slots.  Replace the URL's for each
# browser to match that of your server.  If you need to add a
# browser to the list simply put a unique part of the environment
# variable, HTTP_USER_AGENT, in the left slot and the associative
# URL in the left slot.  If no match for the browser is found then
# the "default" entry is used (don't delete the default entry and
# don't put a comma after it either).  Have fun!
#
# IMPORTANT: be sure your URL entries begin with a '/'.
#
%browsers = 
(
    # MSIE (list MSIE in front of Mozilla since MSIE includes
    # "Mozilla" in its agent string- truly a Netscape wannabee)
    "MSIE", "/cgi/library/agent/test/msie_enhanced.html",
    # Netscape browsers.  1.0 can't do tables.  You can collapse
    # all of the Netscape browsers into a single entry by using
    # the following line instead of the multiple lines below.
    # "Mozilla", "/cgi/library/agent/test/netscape_enhanced.html",
    "Mozilla/1.0", "/cgi/library/agent/test/non_enhanced.html",
    "Mozilla/1.1", "/cgi/library/agent/test/netscape_enhanced.html",
    "Mozilla/2", "/cgi/library/agent/test/netscape_enhanced.html",
    "Mozilla/3", "/cgi/library/agent/test/netscape_enhanced.html",
    # Default, don't remove the entry
    "default", "/cgi/library/agent/test/non_enhanced.html"
); 
#
# end browser configuration
#
############################################################

$home_ref = "http://$ENV{'SERVER_NAME'}";
$agent = $ENV{'HTTP_USER_AGENT'};

# if no agent environment variable is present, redirect to default
if ($agent eq "") {
  print "Location: $home_ref$browsers{'default'}\n\n";
  exit(0);
}

# loop through the browsers
foreach $key(keys %browsers) {
  # skip the "default" entry.
  if ($key ne "default") {
    if ($agent =~ /$key/) {
      print "Location: $home_ref$browsers{$key}\n\n"; 
      exit(0);
    }
  }
}

# no match, print out default
print "Location: $home_ref$browsers{'default'}\n\n";
exit(0);

##############################################################################
# eof agent.pl

If you are unfamiliar with the CGI standard, or would like to learn more about the Common Gateway Interface, the following URL is an excellent resource:

http://hoohoo.ncsa.uiuc.edu/cgi/


How to Install Remote Agent Detection

To install remote agent detection on your Virtual Server you will need to do three things:

  1. Modify your Server Resource Map and Access Configuration
    In order for the remote agent detection script to work properly, you server must be configured to recognize "agent.pl" as a valid directory index entry. Also, your server must allow the "agent.pl" to execute. If you are an A Little Technology Shoppe, LLC Virtual Server client, then these steps are easy (and outlined below. If you are not an A Little Technology Shoppe, LLC client (sign up!) then you should be able to follow along and simply modify the instructions to fit your server environment.

        Configuring Multiple Directory Index Entry

    • Telnet to your server and open the server resource map configuration file, "srm.conf", file found in your "www/conf" area.

      Edit the "DirectoryIndex" such that "agent.pl" is the first definition entry. This will instruct the server to look for a "agent.pl" in a directory and if it is not found to look for the second file. Thus in the following example,

          DirectoryIndex   agent.pl index.html

      the server will check for the existence of the "agent.pl" script and if it is not present will then use the "index.html" file if present.

        Configuring Execution Access

    • In order for the HTML source to execute the "agent.pl" cgi, your server must be configured such that it recoginizes cgi execution outside of the normal "cgi-bin" area. If you are a virtual server client (each of whom controls their own NCSA based httpd server with a complete set of configuration files) you can simply drop a ".htaccess" file into the directory where you store the "agent.pl" cgi. A sample ".htaccess" file is shown below:
            Options   Indexes FollowSymLinks Includes ExecCGI
            AddType   application/x-httpd-cgi .pl
            
  2. Download the Remote Agent Detection CGI source code
    You will need to download the file, agent.pl.

          If you are an A Little Technology Shoppe, LLC customer

    1. login to your Virtual Server.
    2. change directories to your home directory (type "cd" and hit return)
    3. type "tar -xvf /usr/local/contrib/agent.tar"

          If you are not an A Little Technology Shoppe, LLC customer (sign up!)

    1. Verify that a directory path "library/agent" exists in your "cgi-bin" area.
    2. Download the source file into your "cgi-bin/library/agent" directory and make sure the mode is set so that it will execute (chmod +x).

  3. Create the HTML Source Associated with each Browser
    Create the HTML versions of your pages that will issued to each type of browser. Be sure to modify the source code (see above) so that each browser is associated with its proper URL.

Once you have completed the installation successfully, you will have a dynamic site that issues HTML source to clients based on the agent type (browser). An example of remote agent detection is given below.

Agent Switch

You should be taken to a page that is customized for your browser, either the "MSIE Enhanced Page", the "Netscape Enhanced Page", or the "Non Enhanced Page".


Copyright © 1996, 1997. A Little Technology Shoppe, LLC. All rights reserved. All brand names and product names used on these web pages are trademarks, or trade names of their respective holders.