Input to CGI Scripts

Now that we have a CGI script producing a web page, how can we provide input to it from the browser to interact with it?

All programs have two ways to receive input when they are started. The first way, which is the most common, is via arguments. The second way is via environment variables. We are going to focus on this second method. Let's write a CGI script that prints out all of its environment variables:

#!/usr/bin/perl

use strict;

use warnings;

 

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

print "<html>\n";

print "<head>\n";

print "<title>Environment Variables</title>\n";

print "</head>\n";

print "<body>\n";

print "<H1>My Environment Variables</H1>\n";

my @keys = keys %ENV;

foreach my $k (@keys) {

    print "<P>$k = $ENV{$k}</P>\n";

}

print "</body>\n";

print "</html>\n";

 

Most of this code looks like our hello.cgi script, so let's focus on the Perl code.

Perl has 3 type of variables, and all three are seen here. The first type is the simple scalar variable, that begins with a '$'. This type of variable holds a single string or a number. The second type is the array or list variable, that begins with a '@'. This type of variable holds an array or list of scalar variables, and can be indexed with a number or iterated(looped) over, like we did with the foreach command. The third type is the hash variable, which begins with a '%'. The hash variable stores values indexed with 'keys'. The %ENV variable is a hash variable because it needs to store each environment variable (the 'key') with its setting (the 'value').

The first line of Perl code, my @keys = keys %ENV;, introduced a new list variable called @keys with a local file scope (this is why 'my' is in front of it) and assigned it to the output of the command 'keys' which returns the list of all of the hash keys in the given hash variable, in this case the %ENV hash variable. The %ENV variable does not need to be introduced, because it is always created for every Perl program.

The second line of Perl code, foreach my $k (@keys) {, executes the foreach command with a newly introduced local file scalar variable my $k, and assigns it a key from the @keys list variable. The foreach command will loop over every key in the @keys variable, assign it to $k, and execute the code within the brackets '{' and '}'.

The third line of code, print "<P>$k = $ENV{$k}<P>\n";, will print the given key $k with its value $ENV{$k} within the '<P>' HTML "paragraph" tags. Here you can see how to index a hash variable with a scalar variable by providing the 'key' within curly brackets. Note that since the result of indexing a hash variable, the value, is a simple scalar variable, the '%' in front of the ENV changes to a '$'.

You can run this CGI script in the terminal window, and it will print all of the environment variables that are currently set within your bash shell:

ubuntu@ubuntu:~$ emacs -nw env.cgi

ubuntu@ubuntu:~$ chmod a+x env.cgi

ubuntu@ubuntu:~$ ./env.cgi

Content-type: text/html

 

<html>

<head>

<title>Environment Variables</title>

</head>

<body>

<H1>My Environment Variables</H1>

<P>LESSOPEN = | /usr/bin/lesspipe %s</P>

<P>LESSCLOSE = /usr/bin/lesspipe %s %s</P>

<P>PWD = /home/ubuntu</P>

...

 

Similar output can be seen by running the printenv command.

Now let's see what the environment of a CGI script looks like! Copy this to /var/www/cgi-bin and then append /cgi-bin/env.cgi to your Raspberry Pi IP address in the URL window of your browser.

ubuntu@ubuntu:~$ sudo cp env.cgi /var/www/cgi-bin/

ubuntu@ubuntu:~$

 

The environment variable that we are interested in is the QUERY_STRING. Do you see it?

Currently the QUERY_STRING is empty, but try this: append /cgi-bin/env.cgi?1073=force&195=cyberknights&3538=robojackets to the IP address of your Raspberry Pi in the URL window of your browser and press 'enter'. Now what does the QUERY_STRING have for a value?

Part of the CGI support in the web server is copying everything after the '?' in the URL to the QUERY_STRING environment variable before executing the CGI script.

Copyright 2020
  • Facebook - Black Circle
  • Twitter - Black Circle
  • YouTube - Black Circle
  • Instagram - Black Circle
Follow us on these platforms