Question: How can I make my 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 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.

The most common kind of server-dependent code is code which accesses files or programs using an absolute path (such as "/home1/u/username/somefile"). Instead of using the absolute path to your home directory ("/home1/u/username/"), 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 = "/home1/u/username/data/guest.html";

You should change it to:

# Path to your guestbook file

$guestbook = "$ENV{DOCUMENT_ROOT}/data/guest.html";

(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.)


AcceptableUse/how-can-i-make-my-cgi-scripts-server-independent (last edited 2015-07-28 09:42:43 by wilhelms)