FUNDAMENTALS OF CGI USING PERL INSTRUCTOR GUIDE

Document Sample
FUNDAMENTALS OF CGI USING PERL INSTRUCTOR GUIDE Powered By Docstoc
					                        FUNDAMENTALS OF CGI
                            USING PERL:
                         INSTRUCTOR GUIDE
                              TABLE OF CONTENTS
Course Description ...................................................................................................................... x
ProsoftTraining Courseware......................................................................................................... x
Course Objectives...................................................................................................................... xii
Classroom Setup ....................................................................................................................... xii
System Requirements ................................................................................................................ xii
Conventions and Graphics Used in This Book........................................................................... xiv

Classroom Setup Guide ......................................................................... Classroom Setup Guide-1


Lesson 1: Application Development Fundamentals                                                                                          1-1

Pre-Assessment Questions........................................................................................................1-2
Overview of the Application Development Process .....................................................................1-3
The Application Development Process .......................................................................................1-3
Platforms, Languages and Protocols..........................................................................................1-9
Client-side Versus Server-side Scripting .................................................................................1-16
Hypertext Transfer Protocol ....................................................................................................1-18
Lesson 1 Review .....................................................................................................................1-22
Lesson 1 Instructor Section ....................................................................................................1-23
Additional Instructor Notes.....................................................................................................1-24


Lesson 2: Introduction to CGI and Perl                                                                                                  2-1

Pre-Assessment Questions........................................................................................................2-2
Web Architecture Overview .......................................................................................................2-3
What Is CGI? ............................................................................................................................2-3
Why Use CGI? ..........................................................................................................................2-6
What Is Perl? ............................................................................................................................2-6
Why Use Perl? ..........................................................................................................................2-6
Lesson 2 Review .......................................................................................................................2-8
Lesson 2 Instructor Section ......................................................................................................2-9
Additional Instructor Notes.....................................................................................................2-10


Lesson 3: Creating Simple Scripts                                                                                                       3-1

Pre-Assessment Questions........................................................................................................3-2
Key Concepts and Syntax .........................................................................................................3-3
Scalar Variables in Perl.............................................................................................................3-4
Lesson 3 Review .....................................................................................................................3-10
Lesson 3 Instructor Section ....................................................................................................3-12


                    ComputerPREP, a division of ProsoftTraining             -   1.800.228.1027      -   1.602.275.7700
                                       www.ComputerPREP.com             -   www.CIWcertified.com
                                           CCN02-CEFCGI-PR-303_toc.doc            -   page 1 of 6
Additional Instructor Notes.....................................................................................................3-13


Lesson 4: Perl Fundamentals                                                                                                              4-1

Pre-Assessment Questions........................................................................................................4-2
Accessing Environment Variables .............................................................................................4-3
Using CGI.pm to Access Environment Variables .......................................................................4-3
If Statement..............................................................................................................................4-5
Logical Expressions ..................................................................................................................4-6
Regular Expressions and Pattern Matching...............................................................................4-6
Perl Arrays ...............................................................................................................................4-7
Passing Values to Functions .....................................................................................................4-8
Associative Arrays ....................................................................................................................4-8
Loops .....................................................................................................................................4-10
Lesson 4 Review .....................................................................................................................4-15
Lesson 4 Instructor Section ....................................................................................................4-17
Additional Instructor Notes.....................................................................................................4-18


Lesson 5: Perl File Input and Output Capabilities                                                                                        5-1

Pre-Assessment Questions........................................................................................................5-2
File IO Using File Handles ........................................................................................................5-3
File Modes ................................................................................................................................5-4
Using Files in Scripts................................................................................................................5-4
Creating a Hit Counter .............................................................................................................5-7
Lesson 5 Review .....................................................................................................................5-10
Lesson 5 Instructor Section ....................................................................................................5-11
Additional Instructor Notes.....................................................................................................5-12


Lesson 6: Controlling Processing and Output                                                                                              6-1

Pre-Assessment Questions........................................................................................................6-2
Introduction to CGI.pm.............................................................................................................6-3
Incorporating HTML into Perl Using CGI.pm .............................................................................6-3
Using CGI.pm to Access Form Data ..........................................................................................6-4
Processing User-Entered Data ..................................................................................................6-6
Using One File to Create and Process an HTML Form ...............................................................6-7
Using Perl to Validate Form Input.............................................................................................6-8
Testing Your Script Offline........................................................................................................6-9
Lesson 6 Review .....................................................................................................................6-15
Lesson 6 Instructor Section ....................................................................................................6-17
Additional Instructor Notes.....................................................................................................6-18


Lesson 7: Saving User-supplied Data to a File                                                                                            7-1

Pre-Assessment Questions........................................................................................................7-2
Saving Form Data to a File .......................................................................................................7-3
Modifying Form Data ................................................................................................................7-4
Pattern Matching Revisited .......................................................................................................7-6
Lesson 7 Review .....................................................................................................................7-12
Lesson 7 Instructor Section ....................................................................................................7-14
Additional Instructor Notes.....................................................................................................7-15




                                           CCN02-CEFCGI-PR-303_toc.doc             -   page 2 of 6
Lesson 8: Reading a File                                                                                                                8-1

Pre-Assessment Questions........................................................................................................8-2
Introduction to Data Reading....................................................................................................8-3
Pattern Matching with Regular Expressions..............................................................................8-3
Substitution .............................................................................................................................8-5
Lesson 8 Review .......................................................................................................................8-7
Lesson 8 Instructor Section ......................................................................................................8-9
Additional Instructor Notes.....................................................................................................8-10


Lesson 9: Introduction to Databases                                                                                                     9-1

Pre-Assessment Questions........................................................................................................9-2
Introduction to Database Programming ....................................................................................9-3
Four Steps to Interacting with Databases .................................................................................9-4
Connecting to Databases ..........................................................................................................9-6
Structured Query Language......................................................................................................9-7
Quoting Revisited .....................................................................................................................9-9
Querying Table and Field Names ............................................................................................9-10
Web Database Query Example ................................................................................................9-11
Lesson 9 Review .....................................................................................................................9-18
Lesson 9 Instructor Section ....................................................................................................9-20
Additional Instructor Notes.....................................................................................................9-21


Lesson 10: Deleting and Inserting Database Records                                                                                    10-1

Pre-Assessment Questions......................................................................................................10-2
Modifying Data in a Database .................................................................................................10-3
The do Method........................................................................................................................10-4
Web Database Control Example..............................................................................................10-4
Lesson 10 Review .................................................................................................................10-15
Lesson 10 Instructor Section ................................................................................................10-16
Additional Instructor Notes...................................................................................................10-17


Lesson 11: CGI Security Issues                                                                                                        11-1

Pre-Assessment Questions......................................................................................................11-2
Type of Attacks .......................................................................................................................11-3
Securing the CGI Script ..........................................................................................................11-3
Securing the Server ................................................................................................................11-4
Securing Form Data ...............................................................................................................11-6
Securing Data Passed to Commands.......................................................................................11-7
Lesson 11 Review .................................................................................................................11-15
Lesson 11 Instructor Section ................................................................................................11-16
Additional Instructor Notes...................................................................................................11-17

Course Assessment                                                                                           Course Assessment-1

Appendixes                                                                                                              Appendixes-1

Glossary                                                                                                                    Glossary-1

Index                                                                                                                            Index-1

Supplemental CD-ROM Contents                                                            Supplemental CD-ROM Contents-1

Handouts: Optional Labs                                                                             Handouts: Optional Labs-1


                                          CCN02-CEFCGI-PR-303_toc.doc             -   page 3 of 6
Handouts: Quizzes                                                                                            Handouts: Quizzes-1

Handout: Course Assessment                                                                 Handouts: Course Assessment-1



List of Labs

Lab   3-1: Creating your first Perl script ......................................................................................3-8
Lab   4-1: Determining browser type and print environment variables.......................................4-12
Lab   5-1: Adding a counter to myscript.pl ..................................................................................5-7
Lab   6-1: Enhancing myscript.pl to create a data entry form ......................................................6-9
Lab   7-1: Creating a file from data entered into a form ...............................................................7-8
Lab   8-1: Retrieving and formatting data from a mini-database ..................................................8-5
Lab   9-1: Using ODBC .............................................................................................................9-12
Lab   9-2: Creating a user query entry box ................................................................................9-13
Lab   10-1: Receiving and processing data from a form .............................................................10-6
Lab   10-2: Deleting records and updating a Web page............................................................10-10
Lab   11-1: Authenticating passwords .....................................................................................11-10


List of Optional Labs

Optional    Lab   1-1: Using HTTP manually .................................................................................1-24
Optional    Lab   2-1: Invoking a CGI program..............................................................................2-10
Optional    Lab   3-1: Experimenting with Perl..............................................................................3-13
Optional    Lab   4-1: Working with associative arrays..................................................................4-19
Optional    Lab   5-1: Reading and testing files.............................................................................5-14
Optional    Lab   6-1: Using the CGI.pm module...........................................................................6-19
Optional    Lab   7-1: Using regular expressions and file handles .................................................7-15
Optional    Lab   8-1: Using complex regular expressions .............................................................8-11
Optional    Lab   9-1: Using the tables method .............................................................................9-25
Optional    Lab   10-1: Updating records and updating a Web page ............................................10-17
Optional    Lab   11-1: Using the DBI Taint attribute..................................................................11-17


List of Quizzes

Lesson    1 Quiz.........................................................................................................................1-28
Lesson    2 Quiz.........................................................................................................................2-12
Lesson    3 Quiz.........................................................................................................................3-15
Lesson    4 Quiz.........................................................................................................................4-22
Lesson    5 Quiz.........................................................................................................................5-17
Lesson    6 Quiz.........................................................................................................................6-23
Lesson    7 Quiz.........................................................................................................................7-19
Lesson    8 Quiz.........................................................................................................................8-13
Lesson    9 Quiz.........................................................................................................................9-27
Lesson    10 Quiz.....................................................................................................................10-23
Lesson    11 Quiz.....................................................................................................................11-19


List of Figures

Figure 1-1: Web clients accessing file from server ...................................................................1-17
Figure 1-2: Simulated code demonstrating how Web pages can use client-side scripting
    to appear like dynamic Web pages ....................................................................................1-17
Figure 1-3: GET request returns named resource or named process output ............................1-19
Figure 1-4: GET returns both message and header; HEAD returns header only.......................1-20
Figure OL1-1: Terminal Preferences dialog box .......................................................................1-24
Figure OL1-2: Connect dialog box ...........................................................................................1-25


                                          CCN02-CEFCGI-PR-303_toc.doc            -   page 4 of 6
Figure   OL1-3: Result of GET request ......................................................................................1-25
Figure   OL1-4: Result of POST request.....................................................................................1-26
Figure   OL1-5: Second result of POST request..........................................................................1-27
Figure   2-1: Browser, server and script interaction.....................................................................2-3
Figure   OL2-1: OptionalLab2-1.htm .........................................................................................2-11
Figure   OL2-2: Result of form submission ................................................................................2-11
Figure   3-1: Myscript.pl output results ......................................................................................3-9
Figure   OL3-1: OptionalLab3-1.pl ............................................................................................3-14
Figure   4-1: MyScript1.pl .........................................................................................................4-14
Figure   IN4-1: Result of OptionalLab4-1.pl...............................................................................4-19
Figure   OL4-1: OptionalLab4-1.pl ............................................................................................4-20
Figure   OL4-2: OptionalLab4-1.pl (cont'd) ................................................................................4-21
Figure   5-1: MyScript2.pl ...........................................................................................................5-8
Figure   5-2: MyScript.pl .............................................................................................................5-9
Figure   IN5-1: Result of die operator ........................................................................................5-13
Figure   IN5-2: Result of newline character appended to die text ...............................................5-13
Figure   IN5-3: Result of using $! variable .................................................................................5-14
Figure   OL5-1: Error message in browser.................................................................................5-15
Figure   OL5-2: OptionalLab5-1.pl ...........................................................................................5-15
Figure   OL5-3: OptionalLab5-1.pl (cont'd) ................................................................................5-16
Figure   6-1: Myscript3.pl as an interview form .........................................................................6-11
Figure   6-2: Results of form submission for accepted candidate ...............................................6-12
Figure   6-3: Results of form submission for rejected candidate.................................................6-12
Figure   OL6-1: Cgipm.pl form ..................................................................................................6-21
Figure   OL6-2: Results of form submission ..............................................................................6-21
Figure   7-1: Myscript4.pl HR interview form.............................................................................7-10
Figure   OL7-1: Employee.dat ...................................................................................................7-17
Figure   OL7-2: OptionalLab7-1.pl display ................................................................................7-17
Figure   8-1: Readdb.pl file in formatted file output .....................................................................8-6
Figure   OL8-1: OptionalLab8-1.pl ............................................................................................8-12
Figure   9-1: Database Interface module .....................................................................................9-3
Figure   9-2: Results of database query .....................................................................................9-13
Figure   9-3: Search Employee Database entry box ...................................................................9-15
Figure   9-4: Data returned from user query .............................................................................9-15
Figure   9-5: Search page with no results found ........................................................................9-16
Figure   OL9-1: OptionalLab9-1.pl ............................................................................................9-26
Figure   10-1: Employee database search and entry form ..........................................................10-9
Figure   10-2: Results screen showing successful entry into employee database ........................10-9
Figure   10-3: Odbc.pl showing new employee entry................................................................10-10
Figure   10-4: Employee Database update screen....................................................................10-12
Figure   10-5: Deletion results ................................................................................................10-13
Figure   10-6: Invalid ID results ..............................................................................................10-13
Figure   OL10-1: Employee Database update screen ...............................................................10-20
Figure   OL10-2: Employee update interface ...........................................................................10-21
Figure   OL10-3: Employee Database update screen with updated data...................................10-21
Figure   OL10-4: Invalid ID results..........................................................................................10-22
Figure   11-1: Microsoft Security Advisor ..................................................................................11-5
Figure   11-2: Employee Database password screen ................................................................11-12
Figure   11-3: Invalid admin name or password result ............................................................11-12
Figure   11-4: Employee Database for authenticated user .......................................................11-13
Figure   OL11-1: OptionalLab11-1.pl ......................................................................................11-18


List of Tables

Table 2-1: Virtual and actual directories for class .....................................................................2-6
Table 3-1: Correct variable syntax ............................................................................................3-5
Table 3-2: Escape sequences ....................................................................................................3-6


                                         CCN02-CEFCGI-PR-303_toc.doc           -   page 5 of 6
Table   4-1:   Environment access methods ...................................................................................4-4
Table   4-2:   Numeric and string operators...................................................................................4-6
Table   5-1:   File modes................................................................................................................5-4
Table   6-1:   CGI.pm HTML-generating methods...........................................................................6-3
Table   7-1:   HTML generation methods and named parameters ...................................................7-5
Table   8-2:   Quantifiers...............................................................................................................8-4


Appendixes

Appendix   A:          Objectives and Locations*
Appendix   B:          Installing Apache Server*
Appendix   C:          Installing Perl on UNIX and Win32*
Appendix   D:          References*
Appendix   E:          A Requirements Document*
Appendix   F:          Works Cited*

* Appendix found on Supplemental CD-ROM




                                          CCN02-CEFCGI-PR-303_toc.doc            -   page 6 of 6