Making Your
CGI-scripts Server Independent
If your scripts contain server-dependent code, that
is, code which is specific to a particular server, you
would need to rewrite code if you ever decided to move
your site to a different web server. One such occasion
would be if you decided to upgrade to or downgrade from a
High Volume account, since these accounts
reside on high volume servers. Another occasion would be
if you ever decided to reuse your scripts for other
accounts, or give your scripts to other people. You
should always try to write code that will run correctly
regardless of what web server it runs on. This makes the
script extremely portable and very easy to maintain.
The most common kind of server-dependent code is code
which accesses files or programs using an absolute path
(such as "/home14/someid/somefile"). Instead
of using the absolute path to your home directory
("/home14/someid"), you should instead use
the DOCUMENT_ROOT environment variable
($ENV{DOCUMENT_ROOT} in Perl) to determine the path of
your files or programs within a script.
For example, if a guestbook script reads:
# Path to your guestbook file
$guestbook =
"/home21/examp9/data/guest.html";
You should change it to:
# Path to your guestbook file
$guestbook =
"$ENV{DOCUMENT_ROOT}/data/guest.html";
If you are writing scripts for others besides yourself
to use, you may wish to take advantage of the many other
environment variables which allow you to write
server-independent code.
Other Commonly used Server-Independent Codes
include:
- AUTH_TYPE
- * only works if you are viewing a password
protected page. If viewing a password
protected web page, this will be the
"authentication type".
- DOCUMENT_NAME
- This file's name.
- DOCUMENT_URL
- This document's URL (path and filename)
- DATE_LOCAL
- The local representation of date and time of
the web server.
(Tue, 03 Mar 1998 21:15:45 EST)
- DATE_GMT
- The date and time of the web server in GMT.
(Wed, 04 Mar 1998 05:15:46 GMT)
- GATEWAY_INTERFACE
- The name and version number of the gateway
software running on this server. (CGI/1.1)
- HTTP_USER_AGENT
- The "user agent" or web browser
that the visitor is using. (Mozilla/4.0)
- LAST_MODIFIED
- The last modification date of the current
document. (Tue, 03 Mar 1998)
- PATH
- The paths available to the "nobody"
user of the web server.
- REMOTE_USER
- * only works if you are viewing a password
protected page. If viewing a password
protected web page, this will be the
"username".
- REQUEST_METHOD
- The method by which the visitor's web browser
is retrieving the web pages. Choices are GET
and POST.
- REMOTE_HOST
- The translated name of your (the visitor's)
machine.
- REMOTE_ADDR
- The IP number of your (the visitor's)
machine.
- SCRIPT_NAME
- The name of the script running.
(/home16/someid/somefile)
- SERVER_SOFTWARE
- The name of the web server software currently
running.
- SERVER_NAME
- This machine's name, or rather, what this
machine thinks its name is.
- SERVER_PORT
- Standard http/web port.
- SERVER_PROTOCOL
- The name and version number of the protocol
running on this server. (HTTP/1.0)
(Note: if you are writing scripts or recipes for use
with procmail, you must use the
HOME environment variable in place of DOCUMENT_ROOT. CGI
environment variables are not available while running
under procmail.)
Copyright © 1997, 1998
MJourney Communications. All rights reserved.
|