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

Post on 04-Jan-2016

214 views 2 download

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

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.

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Web Clock

Changes from one viewing to another

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Simple Survey

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Simple Game

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Quiz

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Search Engine

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Database access

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

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

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>

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Examples

• Web Clock

Changes from one viewing to another

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

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI In Brief

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

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

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

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Gateway to a Database

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

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

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

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

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;

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Client Requests a Form

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Client Renders Form

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Viewer Fills in Form• Client Sends Form to Server

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

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"

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI In Brief

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

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Form Processing

• Server Sends HTML Document to Browser• Client Renders HTML

CSU - DEOIntroduction to CGI - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI and Form Processing

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

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

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