Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


CGI: Input

CGI scripts get their input mainly from environment variables and standard input (when using the POST method). These environment variables are set when the server executes the gateway program.

The environment variable QUERY_STRING is everything that follows the question mark in the URL. This information could be added either by an ISINDEX document, or by a form using the GET method.

In the POST method, form data are read from stdin. The server will NOT send you an EOF on the end of the data, instead you should use the environment variable CONTENT_LENGTH to determine how much data you should read from stdin.

	$method = $ENV{'REQUEST_METHOD'};

#	Get the query string, & decode it.

	if	( $method eq 'POST' )	{

#		The POST method means that the form data
#		are coming in through standard input.

	 	read(STDIN, $_, $ENV{'CONTENT_LENGTH'});
		}
	elsif	( $method eq 'GET' )	{

#		The GET method means that the form data
#		are coming in through an environment
#		variable.

		$_ = $ENV{'QUERY_STRING'};
		}
	else	{
		print	"UnExpected method:	$method";
		exit	(1);
		}
#	Data have been presented as text between
#	ampersands, in the form name=value

	foreach $_	(split(/&/))	{

#		This string is encoded in the standard
#		format of changing spaces to +, and
#		encoding special characters with %xx
#		hexadecimal encoding.

		# Convert plus's to spaces
		$in[$i] =~ s/\+/ /g;

		# Split into key and value.
		($key, $val) = split(/=/,$in[$i],2);
				  
		# Convert %XX from hex numbers to
		#	alphanumeric
		$key =~ s/%(..)/pack("c",hex($1))/ge;
		$val =~ s/%(..)/pack("c",hex($1))/ge;

		# Associate key and value
		$in{$key} .= "\0" if (defined($in{$key})); 
		$in{$key} .= $val;
		}

Note that the last 2 lines of code append values having the same name. This may happen, for example, when the HTML form contains multiple OPTIONs in a SELECT tag, and the user selected more than one. Or when the user selects multiple radio buttons. Or when some of the form elements have the same name (unusual, but not impossible). So the script will have to know about these cases, and separate out multiple values, e.g. using split('\0').

Normally you will not write code such as the above into every CGI program, but use one of the libraries. A very common approach is to use ReadParse from cgi-lib.pl. Let us suppose we wanted to echo the values selected from the following form:

The following code is a (barebones) way to do it:
#!/bin/perl
require	'/home/web/public_html/Software/Perl/cgi-lib.pl';

	&ReadParse	;

	print	"Content-type: text/html\n\n";

	foreach	(split('\0',$in{'options'}))	{
		print	"$_\n";
		}


Up to => Home / Authoring / CGI




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers