17 Years Successfully Designing, Hosting and Promoting Websites



XML Sitemap

Websites Online is honored to be a

And winner of the


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 "/www26/web/someid/somefile"). Instead of using the absolute path to your home directory ("/www26/web/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 = "/www23/web/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:

* only works if you are viewing a password protected page. If viewing a password protected web page, this will be the "authentication type".
This file's name.
This document's URL (path and filename)
The local representation of date and time of the web server.
(Tue, 03 Mar 1998 21:15:45 EST)
The date and time of the web server in GMT.
(Wed, 04 Mar 1998 05:15:46 GMT)
The name and version number of the gateway software running on this server. (CGI/1.1)
The "user agent" or web browser that the visitor is using. (Mozilla/4.0)
The last modification date of the current document. (Tue, 03 Mar 1998)
The paths available to the "nobody" user of the web server.
* only works if you are viewing a password protected page. If viewing a password protected web page, this will be the "username".
The method by which the visitor's web browser is retrieving the web pages. Choices are GET and POST.
The translated name of your (the visitor's) machine.
The IP number of your (the visitor's) machine.
The name of the script running. (/www26/web/someid/somefile)
The name of the web server software currently running.
This machine's name, or rather, what this machine thinks its name is.
Standard http/web port.
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.)

Search this site by typing in your search term then click the search button.

Custom Search


Hit Counter Since July 17, 2006


Please contact the Webmaster with comments about this site.
Copyright 1997-2015 by Websites Online, Inc., all rights reserved. Text, graphics, and HTML code for this site  are protected by US and International Copyright Laws, and excluding Internet search engines and directories,  may not be copied, reprinted, published, translated, hosted, or otherwise distributed by any means without permission.