Search  |  Contact  |  Site Map  |  Print
FastVirtual Web Hosting & Internet Services Customer Care  
Web Hosting  |  Domain Names  |  Web Site Builder  |  Internet Access  |  Support Center  |  About FastVirtual

Writing Portable CGI

Tips and guidelines so you can ensure
your CGI scripts are portable.

When developing CGI scripts, it is best to ensure configuration requirements are kept to a minimum. By using dynamic environment variables as opposed to absolute paths, you can make your scripts portable, thereby reducing script configuration requirements, even if the script is moved to a different system.

'DOCUMENT_ROOT'

Avoid absolute paths to supporting files (i.e. /usr/home/yourdomain.com/filename.txt). Use $ENV{'DOCUMENT_ROOT'}. By using the DOCUMENT_ROOT environment variable, your script will not be affected by changes to the server environment.

Description Path
Home directory: Absolute path:
/usr/home/yourdomain.com

Recommended path:
$ENV{'DOCUMENT_ROOT'}/..

Standards-compliant approach:
$homedir = $ENV{'DOCUMENT_ROOT'};
$homedir =~ s/\/htdocs$// || die;
Document root: Absolute path:
/usr/home/yourdomain.com/htdocs

Recommended path:
$ENV{'DOCUMENT_ROOT'}
"cgi-bin" directory: Absolute path:
/usr/home/yourdomain.com/htdocs/cgi-bin

Recommended path:
$ENV{'DOCUMENT_ROOT'}/cgi-bin

'SERVER_NAME'

Avoid absolute paths to the domain name. The domain name of the server is available via $ENV{'SERVER_NAME'}. By using the SERVER_NAME environment variable, you can avoid having to hard code the name of the server throughout the script. Your script will also be unaffected should the domain name change at a later date:

$myservername = $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'};

'SCRIPT_NAME'

Aviod absolute paths to the script name. The name of the script is available via $ENV{'SCRIPT_NAME'}. By using the SCRIPT_NAME environment variable, you can avoid having to hard code the name of the script throughout your script. Your script will also be unaffected should you choose to rename it at a later date:

$myserver = $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'};
$myurl = "http://$myserver$ENV{'SCRIPT_NAME'}";

'HTTPS'

Avoid hard coding "http://" or "https://" into the script. Use $ENV{'HTTPS'}. By using the HTTPS environment variable, your script will function correctly both on a normal web server, and on a secure server, and will not be affected should you choose to move it between these environments:

$myserver = $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'};
$myhttp = $ENV{'HTTPS'} ? "https://" : "http://";
$myurl = "$myhttp$myserver$ENV{'SCRIPT_NAME'}";

Additional Environment Variables

There are many more environment variables that you can use. For details of all available variables, please visit http://www.yourdomain.com/cgi-bin/env.pl (replacing "yourdomain" with your actual domain name). This script was provided with your account.

Web Hosting  |  Domain Names  |  Web Site Builder  |  Internet Access  |  Support Center  |  About FastVirtual  |  Articles  |  Search  |  Contact  |  Site Map
Top of Page FastVirtual, Inc. All Rights Reserved.  Privacy Policy  |  Web Site Usage Terms  |  General Service Agreement