web forms and cgi scripts
DESCRIPTION
Web forms and CGI scripts. Dr. Andrew C.R. Martin [email protected] http://www.bioinf.org.uk/. Aims and objectives. Understand how the web works Be able to create forms on HTML pages Understand how CGI scripts can create pages and obtain data from forms - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/2.jpg)
Aims and objectivesUnderstand how the web worksBe able to create forms on HTML
pagesUnderstand how CGI scripts can
create pages and obtain data from forms
Be able to write a CGI script to process data from a form
![Page 3: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/3.jpg)
Internet and Web
Internet The Web
The Web is just one applicationof the Internet
![Page 4: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/4.jpg)
The Internet
FTP
News
The Web
Remoteaccess
RPC
TheInternet
![Page 5: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/5.jpg)
Names and addressesEach computer has a unique IP
addresse.g. 128.40.46.27
Numbers are difficult to remember!Hierarchical domain name scheme
e.g. www.biochem.ucl.ac.uk
Names are mapped to numbers using Domain Name Service (DNS)
![Page 6: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/6.jpg)
How does the web work?The World Wide Web was
developed to share text via hyperlinks between documents on the same or different servers.
![Page 7: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/7.jpg)
How does the web work?
Enter a URL
Send domain name to DNS server
Obtain IP address
Connect to web server
Send request for page to web server Web server sends page to client Client scans document for embedded images, etcRepeat connection and request for each embedded item
Close connection to web server
![Page 8: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/8.jpg)
How does the web work?'Forms' allow information to be sent
to the web server for processing.
The web server collects the information and dispatches it to a CGI script for processing.
The program outputs information to create a new web page which is returned to the web server and thence to your web browser.
![Page 9: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/9.jpg)
CGIScript
How does the web work?
Send request for page to web server
Pages
ExternalPrograms
RDBMS
Web browserWeb server
CGI can extract parameters sent with the page request
![Page 10: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/10.jpg)
What is CGI?Common Gateway Interface
the standard method for a web server to interact with external programs
Can be implemented in any language
Perl scripts written to interact with a web server are often called CGI scripts
The script's standard output is returned by the web server to your web browser
![Page 11: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/11.jpg)
What is CGI?CGI.pm is a Perl module to make it
easy to interact with the web server
The majority of forms-based web pages use CGI.pm written by a Bioinformatician, Lincoln
Stein
![Page 12: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/12.jpg)
Creating a formA simple form:
<form method="post" action="/cgi-bin/clustalw/clustalw.pl">
<p>Enter your sequences in FASTA format:</p><p><textarea name="seqs" rows="20" cols="80"></textarea></p><p>To submit your query, press here:
<input type="submit" value="submit sequences" /></p>
<p>To clear the form and start again, press here:
<input type="reset" value="clear form" /></p></form>
How data aresent to the
server
The CGI scriptto be run on the
server
Buttons andtext boxes
![Page 13: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/13.jpg)
![Page 14: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/14.jpg)
CGI Scripts
Note the location of the scriptthe CGI script will reside on the same
machine as the web page
can also use a full URL
<form method="post" action="/cgi-bin/clustalw/clustalw.pl">
![Page 15: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/15.jpg)
Post and get
‘get’
Used where small amounts of data are to be sent
Data are sent as part of the URL
http://acrmwww.biochem.ucl.ac.uk/cgi-bin/foo.pl?seqid=P00001&format=xml
<form method="get" action="/cgi-bin/clustalw/clustalw.pl">
![Page 16: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/16.jpg)
Post and get
‘post’
Used where larger amounts of data are to be sent
Data sent separately from URL
<form method="post" action="/cgi-bin/clustalw/clustalw.pl">
![Page 17: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/17.jpg)
Form elements
Submit and reset
<input type="submit" value="submit sequences" /><input type="reset" value="clear form" />
Submit: submit formreset: clear form
Text renderedon buttons
![Page 18: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/18.jpg)
Form elements
<textarea>
A (large) box for text entryrows= and cols= attributes
specify size of boxname= gives a name for the CGI
script to use for the dataMust have a </textarea>
Any text in between will appear as default text in the box
<textarea name="seqs" rows="20" cols="80"></textarea>
![Page 19: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/19.jpg)
Form elements
Checkbox
Creates a tick box
If the checkbox is clicked, the name/value pair is sent to the server
<input type=‘checkbox’ name=‘foo’ value=‘bar’ />
checked=‘1’ pre-ticks a box
![Page 20: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/20.jpg)
Form elementsRadio buttons
Radio buttons are grouped by name
One name/value pair sent to server
<input type='radio’ name=‘foo’ value=‘bar’ />
checked=‘1’ pre-ticks a box
![Page 21: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/21.jpg)
Form elements
Text boxes
Creates a one-line text boxAll attributes other than name=
are optional
<input type=‘text’ name=‘foo’ value=‘bar’
size=‘x’ maxlength=‘y’/>
![Page 22: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/22.jpg)
Form elements
Pull-down menus
size=‘n’ gives scrolling list
multiple=‘1’ allows multiple selections
<select name=‘foo’ size=‘1’><option>bar1</option><option>bar2</option></select>
![Page 23: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/23.jpg)
Form elementsVarious other form elements
available
All support several other attributes.
![Page 24: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/24.jpg)
Creating CGI scriptsHTML <form> tag specifies the script
that should be run in the action= attribute
CGI scripts live in a specific directory. Typically/var/www/cgi-bin
/home/httpd/cgi-bin
Web server can be configured to allow CGI scripts in the same directory as HTML pages or elsewhere
![Page 25: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/25.jpg)
Creating CGI scriptsMust extract data sent with GET
or POST methodsThis involves quite complex
unpacking and decodingAll handled (in Perl) by CGI.pm
![Page 26: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/26.jpg)
CGI.pmVery powerful and flexible
Very easy to use
Also provides methods to write pagesNot particularly worthwhile!
![Page 27: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/27.jpg)
OO SyntaxCGI module (like many Perl
modules) uses object orientated syntax
Create a new CGI object
The object's methods are then accessed using the arrow (->) syntax.
$cgi = new CGI;
print $cgi->header();
![Page 28: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/28.jpg)
A simple CGI script
#!/usr/bin/perluse CGI;$cgi = new CGI;print $cgi->header();print <<__EOF;<html><head> <title>Hello World!</title></head><body><h1>Hello World!</h1></body></html>__EOF
AccessCGI.pm
Create a CGI object
Print HTML
Print HTTPheader
![Page 29: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/29.jpg)
A simple CGI scriptCGI module used only to create an
HTTP headerstandard header sent to the web
browser so it knows the type of data which follows
The most useful method provided by the CGI module is the ->param() method – this is used to obtain data from a web page.
![Page 30: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/30.jpg)
Another CGI script
#!/usr/bin/perluse CGI;$cgi = new CGI;$val = $cgi->param('id');print $cgi->header();print <<__EOF;<html><head> <title>Print ID Parameter</title></head><body>__EOFprint “<p>ID parameter was: $val</p>\n”;print “</body></html>\n”;
![Page 31: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/31.jpg)
Names and valuesNormally only one value for each
form element You use a different name= attribute
for each item
Checkboxes and selection lists can return multiple name/value pairs for the same name.
![Page 32: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/32.jpg)
Obtaining multiple values
#!/usr/bin/perluse CGI;$cgi = new CGI;@values = $cgi->param('id');print $cgi->header();print <<__EOF;<html><head> <title>Print ID Parameter</title></head><body>__EOFprint “<p>ID had parameters:</p>\n<ul>\n”;print “<li>$val</li>\n” foreach $val (@values);print “</ul>\n</body></html>\n”;
![Page 33: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/33.jpg)
Accessing external programsOften need to access another program
(e.g. BLAST) from your CGI scriptRun a program from a Perl script:
Note:Example 1: Must use double inverted
commas to interpolate variablesExample 2: Alternatively use back-
ticks
system(“/usr/local/bin/myprog $param”);
$result = `/usr/local/bin/myprog $param`;
![Page 34: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/34.jpg)
Accessing external programsCGI scripts and the programs they
spawn run as the 'nobody' user.Search path and environment
variables may well not be what you expect!
![Page 35: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/35.jpg)
Accessing external programsSet any environment variables
you need in your CGI script:
Use the full path to any external programs(possible exception of standard Unix-like
commands)
$ENV{'varname'}='value';
![Page 36: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/36.jpg)
Temporary filesOften need to create temporary
working filesMust ensure that the filename is
uniqueMore than one person could hit your
web server at the same time!Use the process ID to ensure
unique filename$filename = “/tmp/cgifile_$$”;
![Page 37: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/37.jpg)
Temporary filesMay need to create a temporary file
to return to the userMost web servers provide a
directory in which such files can be written
![Page 38: Web forms and CGI scripts](https://reader036.vdocuments.net/reader036/viewer/2022062305/5681529a550346895dc0c017/html5/thumbnails/38.jpg)
SummaryForms are used to send data to the
web serverGET and POST methods for
transferring dataCGI scripts can simply serve web
pagesno data obtained from a form
CGI scripts can obtain data from a page and run external programs