![]() |
Exploiting the Information World
|
|
For each exercise that you complete during class you must make record your results to show the TA (or show the TA your screen). If you don't have time to complete everything during class you must record your results and give them to the TA by the next class meeting. |
If you got a Perl CGI program running last time (as part of Exercise 12), skip this and go to: 2. Getting at the query string.
These instructions assume you are using the Xitami web serverThe file simple.pl is a simple perl CGI program that can help you learn how to install and run a perl based CGI with the Xitami web server. The CGI program simply sends back some HTML, it doesn't ever look for input from the browser (a query string). Here is a list of the steps necessary to install this program.
simple.pl.
perl simple.pl. You should see the following output:
Content-type: text/html <HEAD><TITLE>Simple CGI in Perl</TITLE></HEAD> <H2>This document was created by a simple perl CGI program</H2> <H4>Here is a list of my favorite integers</H4> <UL> <LI>0 <LI>1 <LI>2 <LI>3 <LI>4 <LI>5 <LI>6 <LI>7 <LI>8 <LI>9 </UL> </HTML></BODY> |
"cgi-bin" in the path. You can copy the program to the
main cgi-bin directory or you can create your own directory
(or complete folder/directory heirarchy) as long as the name of one
folder/directory is "cgi-bin".
On my computer, I've set the main HTML directory to be
c:\dlh\it\web instead of the default directory
c:\Xitami\Webpages. I've also created the directory
c:\dlh\it\web\eiw\cgi-bin as a place to put CGI programs.
Once I've put simple.pl in to the folder
c:\dlh\it\web\eiw\cgi-bin. I can run the program by pointing the
browser to the URL: http://127.0.0.1/eiw/cgi-bin/simple.pl.
#!/perl/bin/perl
http://127.0.0.1/eiw/cgi-bin/simple.pl. The URL you
need depends on the path from the main HTML directory to your program.
You must install the program somewhere below (within) the main
HTML directory or the web server will not be able to find it. The web
server looks for all resources (files) within the main HTML directory.
To change (or simply find out what the current setting is) the location of
the main HTML directory you should go to the web server administration
page using the URL: http://127.0.0.1/admin. Note that you need
to know the username and password you entered when installing the web server.
If you have problems - check this list:
simple.pl program working in a DOS window?
If not - this is the first thing to fix.
#!/perl/bin/perl
/perl/bin/perl.exe you need
to change the first line to match the location of your perl interpreter.
http://127.0.0.1/.
cgi-bin).
Inside your perl program you can get at the query string with the following code (this works for GET requests, not POST):
$q = $ENV{"QUERY_STRING"};
In that above code, if a query was sent as part of the GET request that started the CGI program, the query string will end up in the variable $q. If there was no query string the variable $q will be empty.
Write a CGI program that sends back to the browser an HTML document that includes whatever was sent in the query string. For example, your document could send back: "<H3>Your query string was ", followed by the actual query, followed by "</H3>".
Write a CGI program that treats the query string as an integer value, and sends back a document that includes a hyperlink back to the same CGI. THe hyperlink should include a query string that has a value that is one larger than the value received. If the program name is "count.cgi" and it is called as "count.cgi?18" it should create a hyperlink that references "count.cgi?19".
Create a CGI program that expects the query string to be the name of a file. The CGI program sends back the contents of the named file. You will need to know how to open and read from a file in perl - here is an example:
# assume the variable $file holds the name of a file
# open the file for reading, creates a "handle" named F we can use
# when reading from the file.
if (! open(F,$file) ) {
# ERROR - open returned FALSE - the open did not work
print("ERROR - FILE $file not found\n");
exit; #terminates the program!
}
# now read one line at a time from F, printing each line out
while (<F>) {
print;
}
# close the file
close(F);
|
Create an HTML form and set the action to point to the CGI program that just sends back the query string. Make sure the form METHOD is GET. Try typing in different values for the form fields and submitting to your CGI.