24
CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the Web Instructor: Joseph DiVerdi, Ph.D.

CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

Embed Size (px)

Citation preview

Page 1: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Introduction to the Common Gateway

Interface(CGI) on the Web

Instructor: Joseph DiVerdi, Ph.D.

Page 2: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI In Brief

• CGI stands for Common Gateway Interface– It allows the web server to communicate with other

programs that are running on the server. For example:

– With CGI, the server causes an external program to execute while passing user-specific data to that program.

– That program processes that data, sends a response back to the server which passes the response back to the browser.

Page 3: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI In Brief

Page 4: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Web Clock

Changes from one viewing to another

Page 5: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Simple Survey

Page 6: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Simple Game

Page 7: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Quiz

Page 8: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Search Engine

Page 9: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Database access

Page 10: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Uses of CGI

• Not limited to previously written documents • Enables web pages to be created on-the-fly

– Based on the Users’ input

• Provide users with information, collect their comments, and respond

Page 11: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Dynamic Page Content

• Page created dynamically via (Perl) script or• Page with Server Side Includes (SSI) or• Page with embedded call to (Perl) script• The result is still an HTML page

Page 12: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Dynamically Created Image

• Web Clock

Page 13: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - 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 14: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Interaction

• Operation is as follows:– Client requests a document which actually is a

CGI program perhaps including some user-specific data

– Server executes requested program passing data received from browser request

– Program completes execution and generates a response

– Server returns response to browser

Page 15: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Interaction

Page 16: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI and Form Processing

Page 17: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Database Interaction

• Combing the power of relational database management systems (RDBMS) with Web is very powerful.

• CGI program is required to decode input, assemble query, send to database, process return data from database, and create return document.

Page 18: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

CGI Gateway to a Database

Page 19: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Preparing to Use CGI

• Create directory "cgi" in "html" directory• CaSe iS vErY iMpOrTaNt!• Ensure cgi directory has "rwxr-xr-x" access• CGI programs must be located here• Ensure programs have "rwxr-xr-x" access• Always test program with telnet client FIRST!• Test using Browser and sample HTML page

Page 20: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Simple But Useful Program

• Create a file "useful.pl"#! /usr/bin/perl -w

print "Content-type: text/html\n\n";

print "Hello, CGI Programmer!<BR>\n";

exit;

• Save file in "cgi"• Ensure program has "rwxr-xr-x" access• Test using telnet client• Test using browser

Page 21: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

More Useful Program

• Modify the file "useful.pl"#! /usr/bin/perl -w

print "Content-type: text/html\n\n";

print "Hello, CGI Programmer!<BR>\n";

print "The current time is ", scalar localtime, "<BR>\n";

print "You are using the computer named: ", $ENV{'REMOTE_HOST'}, "<BR>\n";

exit;

Page 22: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Simple Shell Program

• Create a file "env.cgi"#! /bin/sh

echo "Content-type: text/html"

echo

env

Page 23: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Using Others' Programs

• Create a form page, e.g., Ice Cream Survey• Include the following HTML

<FORM METHOD=POST ACTION="http://www.xtrsystems.com/cgi/formmail.pl"

<INPUT TYPE=HIDDEN NAME="send_feedback_to" VALUE="your_email_address">

<INPUT TYPE=HIDDEN NAME="subject" VALUE="your_email_subject">

Page 24: CSU - DCE 0723 - Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the

CSU - DCE 0723 - Advanced PerlCGI Operation - Fort Collins, CO

Copyright © XTR Systems, LLC

Using Programs

• Install formmail.pl in your account on linus• Download from class Materials Page• Upload to "cgi" directory• Check program access• Modify form HTML• Test with browser• Modify program (very carefully!!)• Have fun