You are on page 1of 13

CGI

COMMON GATEWAY
INTERFACE
Dynamic Web

 When the Web first started, there were only static


HTML pages.
 The internet had been around for some time already,
but it was only after the introduction of HTML.
 A lot has happened since then.
 We would like to place the birth of the dynamic web
to when CGI, Common Gateway Interface, was first
introduced in 1993.
 CGI was a way to let a website run scripts (usually
Perl scripts back then) on the web server and display
the output.
Common Gateway Interface

 An interface that allows the Web Server to launch


external applications that create pages dynamically
 A kind of «double client-server loop»

 A CGI program can be written in any programming


language
Cont..

 CGI programs can be written in any language.


 Perl is a very common language for CGI
programming as it is largely platform independent
and the language's features make it very easy to write
powerful applications.
 However, some CGI programs are written in C, shell
script, or other languages.
 It is important to remember that CGI is not a
language in itself.
How CGI Scripts Work
Cont..

1. The Web surfer fills out a form and clicks, “Submit.” The
information in the form is sent over the Internet to the Web
server.
2. The Web server “grabs” the information from the form and passes
it to the CGI software.
3. The CGI software performs whatever validation of this
information that is required. For instance, it might check to see if
an e-mail address is valid. If this is a database program, the CGI
software prepares a database statement to either add, edit, or
delete information from the database.
4. The CGI software then executes the prepared database statement,
which is passed to the database driver.
5. The database driver acts as a middleman and performs the
requested action on the database itself.
6. The results of the database action are then passed back to the
database driver.
Cont..

7. The database driver sends the information from the database to


the CGI software.
8. The CGI software takes the information from the database and
manipulates it into the format that is desired.
9. If any static HTML pages need to be created, the CGI program
accesses the Web server computer’s file system and reads, writes,
and/or edits files.
10. 10. The CGI software then sends the result it wants the Web
surfer’s browser to see back to the Web server.
11. 11. The Web server sends the result it got from the CGI software
back to the Web surfer’s browser.
Basic Structure of CGI Script

 Step 1: Initialization
 Check REQUEST_METHOD.

 Parse string and extract variables depending on “GET” or


“POST”.
 Check CONTENT_TYPE, to find out if the string is URL-
encoded.
 Step 2: Processing
 Process the input data.

 Output the results


Cont..

 Step 3: Termination
 Release the system resources.

 Terminate the program.


Advantages

 CGI programs are portable and work on a wide variety of web


servers and platforms.
 They are language independent. You can write then in any language
and make them work in a wide variety of environments. Some of the
programming languages good enough to write your CGI scripts are
Perl, UNIX Shell, C language, Visual Basic, Python, C# and Java.
 They provide simple interfaces for the clients to interact with the
web servers.
 They are scalable programs and you can use them to perform simple
tasks in the application layer as well as more complex tasks such as
interacting with databases and shopping carts.
 They provide interactivity to a web application and enhance user
experience.
 CGI programs are cost effective. By using them businesses can lower
their development and maintenance costs.
Disadvantages

 The CGI programs are memory intensive programs. Every time a request is
made to a server, it has to launch the CGI program. If they are written in a
scripting language, the interpreter for the scripting language has to
evaluate the entire script to execute the CGI program each time the
program is initiated. If the server has busy traffic, repeated client side
requests would consume a great deal of server resources and impact server
performance. This problem is minimized to some extent by CGI scripts
written in a compiled or interpreted language like C where there is no need
for an interpreter to read the CGI program code separately.
 CGI programs are not easy to write. They require complex programming
and designing skills on the part of the web developers since a lot depends
upon how they are implemented in the server environment.
 If proper care is not taken, CGI programs may compromise server security.
 Most of the CGI programs are well known, free and easily available. Their
strengths and vulnerabilities are known to most web developers. This often
result in their exploitation and misuse.
 Thank You…

You might also like