next up previous contents index
Next: Cookies Up: CGI Scripting Previous: Standard Input   Contents   Index


Query String

The query string is basically the same as the standard input except the query string cannot hold as much information as standard input can, the query string becomes part of the URL, and you use the GET method instead of POST. This is good when you want to bookmark a CGI page, but it is bad when you want to transmit information securely. Not that information transmitted in the query string is transmitted in a different way then standard input, but if it is on the query string the user can see the information. While the program has to read in standard input, the query string is contained in the environment. To see the query string you can create the file /var/www/cgi-bin/query.cgi:

#!/usr/bin/perl

$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        
        push (@qlist, $name);
        $qinput{$name} = $value;
}

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

print "<html>\n";
print "<head>\n";
print "<title>Query String</title>\n";
print "</head>\n";
print "<body bgcolor=ffffff>\n";

if (@qlist) {
    print "<h1>Query String</h1>\n";
    foreach $x (@qlist) {
	print "$x=$qinput{$x}<br>\n";
    }
}

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

To send information to the query string you can change the HTML for standard input so that the form line says this:

<form action=/cgi-bin/query.cgi method=get>

You can also type the URL directly. Try some of the following:

http://localhost/cgi-bin/query.cgi?a=1&b=2&c=3
http://localhost/cgi-bin/query.cgi?search=dog+cat+mouse
http://localhost/cgi-bin/query.cgi?text1=apple&text2=banana


next up previous contents index
Next: Cookies Up: CGI Scripting Previous: Standard Input   Contents   Index
Joseph Colton 2002-09-24