1 / 34

Introduction to the Common Gateway Interface (CGI)

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.

shilah
Download Presentation

Introduction to the Common Gateway Interface (CGI)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi, Ph.D., M.B.A.

  2. CGI Examples • Web Clock Changes from one viewing to another

  3. CGI Examples • Simple Survey

  4. CGI Examples • Simple Game

  5. CGI Examples • Quiz

  6. CGI Examples • Search Engine

  7. CGI Examples • Database access

  8. 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

  9. 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

  10. Dynamic Image Inclusion <HTML> <HEAD> <TITLE>Digital Clock Demo</TITLE> </HEAD> <BODY> <IMG SRC="/cgi/digital_clock.pl"> </BODY> </HTML>

  11. CGI Examples • Web Clock Changes from one viewing to another

  12. 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

  13. CGI In Brief

  14. 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

  15. 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

  16. 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

  17. CGI Gateway to a Database

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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;

  23. CGI Form Processing • Client Requests a Form

  24. CGI Form Processing • Client Renders Form

  25. CGI Form Processing • Viewer Fills in Form • Client Sends Form to Server

  26. 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

  27. 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"

  28. CGI In Brief

  29. 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

  30. CGI Form Processing • Server Sends HTML Document to Browser • Client Renders HTML

  31. CGI and Form Processing

  32. 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

  33. 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

  34. 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

More Related