CGI Environment Variables

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

Regardless of whether any form data is being passed to the CGI program or not, every CGI application receives information about both the browser and the server through environment variables.

The environment variables defined for CGI applications provide information such as the following:


Where on the network the browser is located
The browser type and what types of documents it understands
The name and version of the server that called the CGI program
Instructions on how to receive and interpret data sent by the browser
A certain set of environment variables are always set by servers abiding by the CGI interface.
Also, a few other environment variables exist which, while not defined in the CGI interface, are often passed to the CGI program.
Perl defines an associative array %ENV that stores the environment variables. The array is keyed by the name of the variable. $forminput $ENV{'QUERY STRING'};

General Variables

This section defines the most general of the environment variables, those that every CGI script will need to be able to read input from the server.

Gateway_interface

GATEWAY INTE R FACE describes the version of the CGI interface being used. The current version of the interface is 1.1, so the value of this variable is almost always CGI/1.1.

Server_protocol

SERVER PROTOCOL describes the version of the HTTP protocol. Most servers understand version 1.0, hence this value is usually HTTP/1.0.

Request_method

REQUEST METHOD is either equal to GET or POST, depending on the method used to send the data to the CGI program.

Variable Storing Input

This section defines those variables that can contain the actual input data being passed from the server to the CGI program.

Pth_info

The user can specify a path value when the user accesses a CGI program by appending a slash (/) followed by the path information.

Path_translated

PATH TRANSLATED is the equivalent value of PATH INFO relative to your file system. If your document root is /usr/local/etc/httpd/htdocs

Query_string

This variable contains input data if the server is sending data using the GET method. It will always contain the value of the string following the URL and separating question mark, regardless of how information is being passed to the CGI program. For example, if you access the following: http://myserver.org/cgi bin/mai1.cgi?static directly from the command line, the value of QUERY STRING is static even though the information is being passed directly and is not a series of name/ value pairs.

Content_type

CONTENT TYPE contains a MIME type that describes how the data is being encoded. By default, CONTENT TYPE will be application/x www form urlencoded. Note that this is the same MIME type normally specified in the ENCTYPE parameter of the <form> tag.

Content_Length

CONTENT LENGTH stores the length of the input being passed to the CGI program. This variable is defined only when the server is using the POST method. For example, if the following is your input string, then CONTENT LENGTH is 24 because there are 24 characters in this string: name=sujea n°ree =music

Server Information

This section defines environment variables that deal with information about the server.

Server_Software

SERVER SOFTWARE is the name and version of the server you are using.

SERVER NAME is the name of the machine running your server.

This is the e mail address of the administrator of your Web server. Not all servers define this variable.

This is the port on which your server is running. The default port for Web servers is 80.

This is the name of the CGI program. You can use SCRIPT NAME to write a CGI program that reacts differently depending on the name used to call it. For example, you could write a CGI program that would display a picture of a cat if SCRIPT NAME was cat or a picture of a dog if SCRIPT NAME was dog. The CGI program would be the same, but you would save it twice: one time as cat and the other as dog.

This is the value of the document root on your server. For example, if your document root is /usr/local/etc/httpd/, the value of DOCUMENT ROOT is /usr/ local/etc/httpd/

This section defines environment variables that deal with information about the client (browser).

This is the name of the machine currently requesting or passing information to your CGI program. For example, if someone at toyotomi. student. harvard.edu is browsing your Web site, the value of REMOTE HOST passed to the CGI program is toyotomi.student.harvard.edu.

This is the IP address of the client machine. For example, if someone at IP address 140.247.187.95 is currently browsing your Web site, the value of REMOTE ADDR is 140.247.187.95.

Both REMOTE HOST and REMOTE ADDR can be useful for writing programs that will respond differently depending on the point from which you are browsing the Web site. REMOTE ADDR tends to be a more reliable value, because not all machines on a TCP/IP network like the Internet have host names, but all of them will have an IP address.

Remote User

If you have entered a valid username to browse an access restricted area on the server, your username is stored in REMOTE USER. By default, REMOTE USES empty. If you access a page with access restrictions, the server first check, REMOTE USER to see if you have authenticated yourself already. If not, responds with a status code of 401. When the client receives this status code, it prompts you for the appropriate information, usually a username and password.

If you enter a valid username and password, your username is stored involved REMOTE USER. The next time you try and access those pages, the serve checks REMOTE USER, finds a value, and enables you to see the appropriate pages.

Remote_group

Some servers have group authentication as well as user authentication. Wit' group authentication, you usually enter your username, and the server looks see whether you belong to the appropriate group. If you do, it stores that value REMOTE GROUP and enables you to access the appropriate documents. Net servers support this form of authentication.

Auth_type

AUTH TYPE defines the authorization scheme being used, if any. The most common authentication scheme is Basic.

Remote_ident

Although the server and CGI program can determine the name of the cited, machine and address currently connected, it normally cannot determine the use, on the client machine accessing your pages. A network protocol known as the IDENT protocol enables querying servers to determine which users from which machines are connecting to your server.

HTTP Variables

Many browsers pass additional information about their capabilities to the server which in turn passes this information to the CGI program in the form of environment variables, These variables are prefixed with HTTP

HTTP_ACCEPT

HTTP_ACCEPT contains a list of MIME types that the browser is capable of interpreting itself. Each MIME type is separated by a comma. For example, a graphical browser that can display both GIF and JPEG images might list the following. image/gif image/jpeg in HTTP ACCEPT HTTP_ACCEPT
Is a useful environment variable for content negotiation. For example, you can determine whether or not a browser is a graphical browser or a text browser by searching HTTP_ACCEPT for an image MIME type.

HTTP_USER_AGENT

This variable stores the browser name, version, and usually its platform. Normally, the format of HTTP_USER_AGENT is Browser/Version (Operating System)

HTTP_REFERER

HTTP_REFERER stores the URL of the previous page that referred you to the current URL.

CGI Program to print the environment variables

In order to write a CGI application that displays the environment variables, you have to know how to do two things:

• Determine all of the environment variables and their corresponding values.
• Print the results to the browser.

In Perl, the environment variables are stored in the associative array %ENV, which is keyed by the environment variable name. Given below is a perl program, env.cgi, which outputs all CGI environment variables.

print "Content-type:text/html\n\n";
print "<html><head>\n;
print "<title>CGI Environment</title>\n";
print "</head>\n";
print "<body>\n";
print "<h1>CGI Environment</h1>\n";
foreach $env_var(keys(%ENV){
print "<B>$env_var</B>=$ENV{$env_var}<BR>\n";
}
print "</body></html>\n";



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.