350 likes | 712 Views
Introduction to the Common Gateway Interface (CGI). Instructor: Joseph DiVerdi, Ph.D., M.B.A. CGI Examples. Web Clock. Changes from one viewing to another. CGI Examples. Simple Survey. CGI Examples. Simple Game. CGI Examples. Quiz. CGI Examples. Search Engine. CGI Examples.
E N D
Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi, Ph.D., M.B.A.
CGI Examples • Web Clock Changes from one viewing to another
CGI Examples • Simple Survey
CGI Examples • Simple Game
CGI Examples • Quiz
CGI Examples • Search Engine
CGI Examples • Database access
CGI Overview • CGI stands for Common Gateway Interface • It is a Specification Which Permits The Web Server Program to Communicate With Other Programs That Are Running On The Server • Web Server Only Knows How to Serve Up HTML Pages • CGI Enables Server to Interact With Other Programs
CGI Overview • Operation is as follows: • Client Requests a Document • Server Recognizes That Document is a Program • Server Executes Program • Supplies Data to Program Obtained From Client Request • Server Receives Program Output • Server ReturnsDocument to Browser • Client Renders Document
Dynamic Image Inclusion <HTML> <HEAD> <TITLE>Digital Clock Demo</TITLE> </HEAD> <BODY> <IMG SRC="/cgi/digital_clock.pl"> </BODY> </HTML>
CGI Examples • Web Clock Changes from one viewing to another
CGI Overview • There are Few Restrictions on What Programming Language is Used in CGI Programs • Perl, Java, Visual Basic, AppleScript, Shell, C++, ... • Perl is the Most Popular Language In Use • CGI Defines The interface Between The Web Server & The Program • In Both Directions • Web Server --> Program • Program --> Web Server
Uses of CGI • Page Serving is Not Limited To Previously Written Documents • Web Pages Can Created On-The-Fly • Can Be Based on The Viewer's Input • Collect Viewer Comments • Respond to Responses
Dynamic Page Content • Page Created Dynamically Via CGI Program • Page With Server Side Includes (SSI) • Page With Embedded Call to CGI Program • The Result is Still an HTML Page • Viewer's Browser Just Sees HTML • CGI Interaction is Behind The Scenes
CGI Database Interaction • Web Interface to Relational Database Management System (RDBMS) • CGI Program is Required to • Decode Viewer Input • Assemble Query • Send Query to Database • Process Data Returned From Database • Create Return HTML Document
Setting Up For CGI Programs • Create cgi directory in html directory • CaSe iS vErY iMpOrTaNt! • Ensure cgi directory has 755 permission • Programs must be placed in this directory • Server is configured only to execute from there • Programs placed in and viewed from other directories will not execute and the program contents will be rendered on the browser
Using CGI Programs • Ensure CGI programs have 755 permission • Always test program with telnet client FIRST! • Login to your account • Navigate to the html/cgi directory • Check if the program executes successfully env.cgi • Then test using Browser http://linus.ulltra.com/~my_account/cgi/env.cgi
Simple Shell Program • Create a file "env.cgi" #! /bin/sh echo "Content-type: text/html" echo env • Remember... • CGI programs can be written in many languages
Simple But Useful Program • Create a file "simple.pl" #! /usr/bin/perl -w print "Content-type: text/html\n\n"; print "Hello, CGI Programmer!<BR>"; exit; • Save file in cgi directory • Ensure program has 755 permission • Test using telnet client • Test using browser
More Useful Program • Modify the file "simple.pl" #! /usr/bin/perl -w print "Content-type: text/html\n\n"; print "Hello, CGI Programmer!<BR>"; print "The current time is ", scalar localtime, "<BR>"; print "You are using the computer: ", $ENV{'REMOTE_HOST'}, "<BR>"; exit;
CGI Form Processing • Client Requests a Form
CGI Form Processing • Client Renders Form
CGI Form Processing • Viewer Fills in Form • Client Sends Form to Server
CGI Form Processing • Client Sends Data to Server • Uses POST Method in HTTP Request POST /cgi/formmail.pl HTTP/1.0 Host: xtrsystems.com ... more headers here ... ice_cream_flavor=chocolate
CGI Form Processing • Server Recognizes URL • URL Points to CGI Program • It knows because of the directory in the URL /cgi/formmail.pl • Server Executes Program • Supplies Form Data to Program • Form Data Received From POST Request "ice_cream_flavor=chocolate"
CGI Form Processing • Program Performs Some Work • Some Combination of the Following • Stores Submitted Data in Database • Creates e-mail Message From Submitted Data • Always Does This • Creates HTML Thank You Page • Sends Program Output to Server • HTML Thank You Page is Program Output
CGI Form Processing • Server Sends HTML Document to Browser • Client Renders HTML
Executing CGI Programs • CGI Programs Are Just Like Other Resources • Can Reside on Your Host • The Host With the Server • Can Reside on Some Other Host • With the Owner's Agreement • Without the Owner's Agreement • Unlike Other Resources • Must Reside in Specific Directories on Each Host • Directory Selected by Webmaster or Site Administrator • Much Harder to Write than HTML Documents
Hands On Work • Create an Ice Cream Survey Form Page • Include the following HTML: <FORM METHOD=POST ACTION="http://xtrsystems.com/cgi/formmail"> • You are using the formmail program • Executing On the xtrsystems.com Server Host
Using Your own Programs • Install formmail.pl in Your Own Account • Download From Course Materials Page • Upload to cgi Directory • Check Program Permission • Modify Form HTML • Test as Described Earlier • Modify Program • Be Very Careful • Have Fun