Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Web Programming

IS0416
The Common Gateway
Interface
Chapter 7
Introduction
 In this chapter, we discuss the basic way to create dynamic
web pages: the Common Gateway Interface (CGI), a
standard for communication between a client and the
server.
 CGI scripts can be written in almost any language.
 Perl is well suited to the types of text processing common
for many tasks, such as search engines and forms interfaces.
Cont . . .
 Other benefits of Perl include portability, ease of
programming, and overall computational power and
performance.
 The Perl module CGI.pm is a useful way to make Perl CGI
script writing quick and easy.
 CGI scripts can do simple things that require no input from
the client, such as displaying the current time or a random
banner when a web page is accessed.
Cont . . .
 Or they can do more complicated tasks involving posted
form data from the client, such as entering a credit card
number, searching a database and returning the
information, and filling out a form.
 First, the basics of how data is passed from browser to
server and from server to browser
Fig: CGI explained
Cont . . .
 Fig. shows what happens during the request and execution
of a CGI program.
 The webserver recognizes a CGI request by the location of
the thing requested (or by the filename extension).
 For example, if we load the URL www.example.com/cgi-
bin/a.cgi into the browser, the webserver contacted,
www.example.com, receives a request such as the
following:
GET /cgi-bin/a.cgi HTTP/
Cont . . .
 The server notices that the directory that contains the thing
requested is cgi-bin.
 It is configured to take the object requested, here a.cgi,
which is a program located on the server, and execute it as
a stand-alone program.
 The program generates standard output.
 This output is in an important format: a header, a blank
line, and the body.
Cont . . .
 The header is a very important piece of information that is
sent back to the browser because it tells the browser how
to render the data that follows.
 The primary piece of information that is sent in the header
is the Content-type.
 If the header contains Content-type: text/plain, the
browser displays the data that follows as plain text.
 If the header contains Content-type: text/html, the browser
treats the data that follows as HTML and renders it
appropriately.
Cont . . .
 And this is what is really important: Programs must output
the header, then a blank line, and then the content to be
displayed.
 The blank line is essential—it tells the browser that the
header is complete and the body is about to begin.
Apache Configuration
 Apache should be correctly configured.
 Two directives in /etc/httpd/conf/httpd.conf should be
checked before you start using CGI.
 The ScriptAlias directive should be set to the proper
directory:
ScriptAlias /cgi-bin/ “/var/www/cgi-bin/”
Cont . . .
 The cgi-bin directory should have the proper options:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
 The AllowOverride directive tells the server how to handle
HTTP authentication with .htaccess files.
 When the directive is set to None, .htaccess files are
ignored.
A First CGI Program

#!/usr/bin/perl
# hello.cgi
print "Content-type: text/plain\n";
print "\n";
print "hello, world!";
Example 2

#!/usr/bin/perl
# hello3.cgi
print "Content-type: text/html\n";
print "\n";
print "<b>hello</b>, <i>world</i>!";
To display the date, time, and hostname on a web page
Use of here document
What Can Go Wrong?
 The following are some common things to check.
• The script is syntactically incorrect (try perl -c script.cgi).
• The first line is not #!/usr/bin/perl.
• There are incorrect file permissions (try chmod a+rx script.cgi or
ls-l).
• There are incorrect directory permissions (be sure the cgi-bin
directory has 755 permissions).
CGI.pm Introduced
 CGI.pm is a Perl module written by Lincoln Stein.
 To use CGI.pm, tell Perl that you want to use it with the
use pragma:
use CGI;
 After Perl executes this statement, your program has access
to all the methods that CGI.pm provides.
 The module can be further refined by specifying a more
convenient programming style, called the standard style:
use CGI ´:standard´;
Example Program
Cont . . .
 The methods used in this program.
• header( ) generates the header "Content-type: text/html\n\n";
CGI.pm adds the extra newline automatically.
• start_html( ) generates the HTML for the start of the web page:
<HTML><HEAD>...; within this, note the use of the following:
• - title => ´A Simple Page´ sets the title within the title tags:<TITLE>A
Simple Page</TITLE>.
• - bgcolor => ´#ffffff´ sets the background color within the body tags,
used with-text, described next.
• - text => ´#520063´ sets the color of the text within the body tags. Used
with the previous-bgcolor, this produces <BODY BGCOLOR="#ffffff"
TEXT="#520063">.
Cont . . .
• h1( ) generates an <H1> tag, and the argument is placed within
the tags; in this example, the HTML produced i<s H1>Hello,
world! again</H1>.
• hr( ) generates <HR>.
• end_html() generates </BODY></HTML>.
CGI.pm HTML Shortcuts
 CGI.pm has functions for most of the commonly used
HTML tags, including
• headings (h1( ), h2( ), h3( ), etc.),
• paragraph breaks (p( )),
• lists (li( ), dl( ), ul( ), dd( ), etc.), and
• text-formatting commands (i( ), em( ), blockquote( )).
Using HTML Shortcuts
 Using these shortcuts is as straightforward as the previous
example.
 Printing a heading:
<H1>Welcome to www.opensourcewebbook.com</H1>
 becomes:
$server_name = `/bin/hostname`;
print h1("Welcome to $server_name")
Cont . . .
 An HTML paragraph, <P>, is formatted as print p( ); (no
arguments), while
print p(´This is a new paragraph´);
 is equivalent to this:
<P>This is a new paragraph</P>
Named Parameters versus Ordered Arguments
 When we executed start_html(), we used named parameters -
for example,-title and -bgcolor.
 CGI.pm provides this way of invoking functions so that we
can pass a lot of data into them in a readable, maintainable
way.
 But there is another way of calling CGI.pm functions,
which is based on the order of arguments.
Cont . . .
 For instance, we could invoke start_html( ) as:
print start_html(-title => ´My Title´);
 or by order, as in:
print start_html(´My Title´);
 CGI.pm is written so that if named parameters are not
used, it knows the order of the arguments.
Information Received by the CGI Program

You might also like