CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the...

34
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi, Ph.D., M.B.A.

Transcript of CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the...

Page 1: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

Introduction to the Common Gateway

Interface(CGI)

Instructor: Joseph DiVerdi, Ph.D., M.B.A.

Page 2: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Web Clock

Changes from one viewing to another

Page 3: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Simple Survey

Page 4: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Simple Game

Page 5: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Quiz

Page 6: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Search Engine

Page 7: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Database access

Page 8: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 9: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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 Returns Document to Browser– Client Renders Document

Page 10: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

Dynamic Image Inclusion

<HTML>

<HEAD>

<TITLE>Digital Clock Demo</TITLE>

</HEAD>

<BODY>

<IMG SRC="/cgi/digital_clock.pl">

</BODY>

</HTML>

Page 11: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Web Clock

Changes from one viewing to another

Page 12: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 13: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI In Brief

Page 14: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 15: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 16: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 17: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Gateway to a Database

Page 18: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 19: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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 Browserhttp://linus.ulltra.com/~my_account/cgi/env.cgi

Page 20: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 21: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 22: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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;

Page 23: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Client Requests a Form

Page 24: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Client Renders Form

Page 25: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Viewer Fills in Form• Client Sends Form to Server

Page 26: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 27: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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"

Page 28: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI In Brief

Page 29: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 30: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Server Sends HTML Document to Browser• Client Renders HTML

Page 31: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI and Form Processing

Page 32: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 33: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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

Page 34: CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

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