next up previous contents index
Next: Query String Up: CGI Scripting Previous: Environment   Contents   Index


Standard Input

In the environment you have a variable $ENV{'CONTENT_LENGTH'} that tells you the number of bytes of standard input that your program can read in. Standard input is generated from an HTML form that used the POST method. If you were interested in seeing the results of the standard input in a readable form you could use the following program stdin.cgi:

#!/usr/bin/perl

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $name =~ tr/+/ /;
        $name =~ s/%([a-f0-9][a-f0-9])/pack("C", hex($1))/eig;
        $value =~ tr/+/ /;
        $value =~ s/%([a-f0-9][a-f0-9])/pack("C", hex($1))/eig;
        
        push (@slist, $name);
        $sinput{$name} = $value;
}

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

print "<html>\n";
print "<head>\n";
print "<title>Standard Input</title>\n";
print "</head>\n";
print "<body bgcolor=ffffff>\n";

if (@slist) {
    print "<h1>STDIN</h1>\n";
    foreach $x (@slist) {
	print "$x=$sinput{$x}<br>\n";
    }
}

print "</body>\n";
print "</html>\n";

exit;

This program is only good if you have an HTML form that points to this file. You could create an html file called /var/www/html/stdin.html that looked like this:

<html>
<head>
<title>STDIN Form</title>
</head>
<body bgcolor=ffffff>
<h1>STDIN Form</h1>
<form action=/cgi-bin/stdin.cgi method=post>
<input type=text name=text1>
<input type=text name=text2>
<input type=text name=text3>
<input type=submit>
</form>
</body>
</html>

From this HTML you could then see what you had typed into the three text boxes when the CGI program ran.



Joseph Colton 2002-09-24