2.cgi, Perl, PHP
2.cgi, Perl, PHP
A standard for how a web server interact and transfer information to an application program (called CGI
script)
A standard for how a web server interact and transfer information to an application program (called CGI
script)
The Common Gateway Interface (CGI) defines a way for client programs (web browsers) to interface
indirectly with applications on the web server.
CGI is a standard interface
A CGI script (executable program) is usually executed to interact with it
Can be written in any programming language
CGI is responsible for generating dynamic content
CGI can call remote databases
Perl is commonly used to write CGI
CGI (Common---Means application is supported by almost all web server and can be
used on all platforms, Gateway----Pathway between server and application programs,
Interface---Provides a well-defined way to call up its features)
Note: CGI is not a programming language
Other server side applications
a) Active server pages (ASP)
b) Java Servlets and Java Server Pages (JSP)
c) Python
d) PHP Hypertext Processor
The CGI process
A user requests the HTTP web server and demands for the URL (Client open connection to
server using a URL)
Client sends request to server known as HTTP request consisting of explicit data
Server processes request – Server launches CGI program – CGI program runs – CGI
program outputs response
Server sends response to client
Client and server close connection
Read explicit data (form data) and implicit data (status code and request headers)
Generate results and send explicit data (data in the form of HTML file), and response
headers as implicit data.
Page | 2
CGI program writes its output to the standard output stream
CGI Data
Data is (in general) collected at the user's browser using an HTML form (Remember form controls
and methods).
This data is submitted to the HTTP server as part of a normal HTTP request using either the GET or
POST methods.
Note: URL, a Uniform Resource Locator is a way of identifying the location of a file on the internet
HTTP GET: Messages sent to a server contain only a URL. Zero or more optional data parameters may
be appended to the end of the URL. The server processes the optional data portion of the URL, if
present, and returns the result (a web page or element of a web page) to the browser.
HTTP POST: Messages place any optional data parameters in the body of the request message rather
Page | 3
than adding them to the end of the URL.
HTTP HEAD: Requests work the same as GET requests. Instead of replying with the full contents of
the URL, the server sends back only the header information (contained inside the HTML section).
#!/usr/local/bin/perl
print "Content-type: text/html","\n\n"; # NOTE: two newlines!!
#
print "<html>\n";
print "<head><title>It Worked</title></head>\n";
print "<body>";
print "<h1>It Worked</h1>\n";
print "If you can read this, it means I know how\n";
print "to get a CGI program working\n";
Page | 6
print "</body>\n";
Perl Example
#!/usr/local/bin/perl
#
# Program to do the obvious
#
print 'Hello world.'; # Print a message
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PHP FORMS