Mastering the Web
Contents
Website Planning Tutorial
Website Design Tutorial
HTML Tutorial
HTML Tables Tutorial
CGI Tutorial
JavaScript Tutorial
Perl Tutorials
CSS Tutorial
Installing a Web Server
Security Tutorial
HTML Cookies Tutorial
Web Tracking Tutorial
Download Free Programs
F.A.Q.

  1. Introduction to CGI
  2. CGI Environment Variables
  3. Using CGI Scripts
  4. Developing a CGI Script
  5. A Form-to-mail CGI Script

Download FWTLogstat1

Download FWTLogstat2

CGI Environment Variables

When a Web browser makes a request to a Web server, the server handles the request to the CGI script and sets a number of environment variables. Every environmental variable has a predefined name so that the script can retrieve its value using that name. Of special interest are those whose names begin with the prefix HTTP_, because these variables' values are obtained from the browser's request. Examples are HTTP_USER_AGENT, HTTP_ACCEPT_LANGUAGE, and HTTP_ACCEPT_CHARSET.

If you want to know the name of the browser that requests a page, it is in the variable HTTP_USER_AGENT. It could be, for instance, "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)". If your script will return a dynamic page, you may be interested in knowing which language (human language) the browser is expecting. This can be found in the variable HTTP_ACCEPT_LANGUAGE.

The variable HTTP_ACCEPT_CHARSET may contain, for example, the string "ISO-8859- 1,utf-8". This means that the browser will understand anything that is encoded in any of these two character sets. So, with environmental variables, Web page coding can be customized to meet the requirements of a specific browser and incompatibilities between browsers can be remedied.

You can see by yourself what are these variables in your host by using this script.

#!/usr/bin/perl
print "Content-type: text/html\n";
print "\n";
print "<b>Environment variables</b><br>\n";
foreach $key (sort(keys %ENV)) {
        print $key, '=', $ENV{$key}, "<br>\n";
}

Upload this script, as "env.pl", to your cgi-bin directory and type in the address box of the Web browser,

www.yourdomain.com/cgi-bin/env.pl

You will see in the browser window a list of the environmental variables that the script received. Alternatively, you can write a simple HTML page.

<HTML>
<HEAD>
<TITLE>Environmental variables of my server</TITLE>
</HEAD>
<BODY>
<A HREF="cgi-bin/env.pl">Click here to see the variables.</A>
</BODY>
</HTML>

The following variables are cited in the CGI specification. Variables that start with the prefix HTTP_ are taken from the header lines sent by the client. Any - (hyphen) characters in the header are changed to _ (underscore) characters.

SERVER_SOFTWARE

The name and version of the Web server software. Example:

Apache/1.3.33 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2

SERVER_NAME

The server's hostname, DNS alias, or IP address. Example:

www.yourdomain.com

GATEWAY_INTERFACE

The revision of the CGI specification to which the server complies. Example:

CGI/1.1

SERVER_PROTOCOL

The name and revision of the protocol that were sent with the request. Example:

HTTP/1.1

SERVER_PORT

The port number to which the request was sent. Example:

80

REQUEST_METHOD

The method with which the request was made. Example:

GET

SCRIPT_NAME

The name of the script being executed. The script may use this information to call itself. Example:

/cgi-bin/env.pl

QUERY_STRING

The information which follows the ? in the URL which referenced this script. Example:

name=Bob+Merrill&place=Brooklyn+N.Y.

REMOTE_ADDR

The IP address of the client machine that is making the request. Example:

200.43.248.253

CONTENT_TYPE

For queries which have attached information, such as HTTP POST and PUT, this is the MIME type of the data. Example:

text/plain

CONTENT_LENGTH

The length (in bytes) of the content to which the previous variable refers. Example:

23

HTTP_ACCEPT

The MIME types that the client will accept. Each item in this list is separated by commas. Example:

text/html,text/plain,image/jpeg

HTTP_USER_AGENT

The browser that is sending the request. Example:

Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; MyIE2)

The Web server may set other variables besides those mentioned in the CGI specification. Example:

DOCUMENT_ROOT=/home/yourdomain

HTTP_ACCEPT_ENCODING=gzip, deflate

HTTP_ACCEPT_LANGUAGE=en

HTTP_CONNECTION=Keep-Alive

HTTP_HOST=www.yourdomain.com

PATH=/usr/local/bin:/usr/bin:/bin

REQUEST_URI=/cgi-bin/env.pl

SCRIPT_FILENAME=/home/yourdomain/cgi-bin/env.pl

SERVER_ADDR=205.219.41.32

SERVER_ADMIN=webmaster@yourdomain.com

Previous | Contents | Next

| HOME | FEEDBACK | BOOKMARK |
Build your Website
© 1999-2008 Hector Castro -- All rights reserved

If your doubt is not answered in this site, please use the
contact form .
I'll answer as soon as posible.
I can help you using instant messaging. To schedule a meeting, please use the
meeting form.
You will find the late news about the free programs offered here on my blog
Free Webmaster Tools
You can get news about updates to my free programs through this
RSS feed.

www.great-web-info.com