Perl/CGI. CS 299 Web Programming and Design



Similar documents
CGI Programming. What is CGI?

7 Why Use Perl for CGI?

Forms, CGI Objectives. HTML forms. Form example. Form example...

10.1 The Common Gateway Interface

CGI An Example. CGI Model (Pieces)

The Basics of Dynamic SAS/IntrNet Applications Roderick A. Rose, Jordan Institute for Families, School of Social Work, UNC-Chapel Hill

Dynamic Content. Dynamic Web Content: HTML Forms CGI Web Servers and HTTP

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

Configuring iplanet 6.0 Web Server For SSL and non-ssl Redirect

Internet Technologies

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

HTML Tables. IT 3203 Introduction to Web Development

COMP 112 Assignment 1: HTTP Servers

HTML Forms and CONTROLS

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Novell Identity Manager

Intell-a-Keeper Reporting System Technical Programming Guide. Tracking your Bookings without going Nuts!

Web Programming. Robert M. Dondero, Ph.D. Princeton University

Viewing Form Results

JISIS and Web Technologies

IBM Tivoli Access Manager for e-business 6.1: Writing External Authentication Interface Servers

Further web design: HTML forms

CGI.pm Tutorial. Table of content. What is CGI? First Program

HTML Basics(w3schools.com, 2013)

Tutorial 6 Creating a Web Form. HTML and CSS 6 TH EDITION

Short notes on webpage programming languages

Chapter 27 Hypertext Transfer Protocol

Real SQL Programming 1

INT322. By the end of this week you will: (1)understand the interaction between a browser, web server, web script, interpreter, and database server.

Designing and Implementing Forms 34

ICT 6012: Web Programming

PHP Debugging. Draft: March 19, Christopher Vickery

SAS/IntrNet 9.4: Application Dispatcher

People Data and the Web Forms and CGI. HTML forms. A user interface to CGI applications

JAVASCRIPT AND COOKIES

FF/EDM Intro Industry Goals/ Purpose Related GISB Standards (Common Codes, IETF) Definitions d 4 d 13 Principles p 6 p 13 p 14 Standards s 16 s 25

OVERVIEW OF ASP. What is ASP. Why ASP

twilio-salesforce Documentation

Form Mail Tutorial. Introduction. The cgi-bin

CTIS 256 Web Technologies II. Week # 1 Serkan GENÇ

Lab - Observing DNS Resolution

Computer Networks. Lecture 7: Application layer: FTP and HTTP. Marcin Bieńkowski. Institute of Computer Science University of Wrocław

Example. Represent this as XML

Introduction to XHTML. 2010, Robert K. Moniot 1

Interfacing the Apache Web Server to APLX

Creating a generic user-password application profile

Contents. 2 Alfresco API Version 1.0

TCP/IP Networking, Part 2: Web-Based Control

Working with forms in PHP

ASP.NET: THE NEW PARADIGM FOR WEB APPLICATION DEVELOPMENT

SQL Injection Attack Lab Using Collabtive

Web. Services. Web Technologies. Today. Web. Technologies. Internet WWW. Protocols TCP/IP HTTP. Apache. Next Time. Lecture # Apache.

Basic tutorial for Dreamweaver CS5

2- Forms and JavaScript Course: Developing web- based applica<ons

Introduction to ASP. Are you sick of static HTML pages? Do you want to create dynamic web pages? Do you

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

Installing AWStats on IIS 6.0 (Including IIS 5.1) - Revision 3.0

07 Forms. 1 About Forms. 2 The FORM Tag. 1.1 Form Handlers

MASTERTAG DEVELOPER GUIDE

WIRIS quizzes web services Getting started with PHP and Java

Basic Website Maintenance Tutorial*

Web Forms for Marketers 2.3 for Sitecore CMS 6.5 and

Xtreeme Search Engine Studio Help Xtreeme

8/9/16. Server-Side Web Programming Intro. The Hamburger Model. To make a Web server based program

Introduction to Ingeniux Forms Builder. 90 minute Course CMSFB-V6 P

If your organization is not already

Interspire Website Publisher Developer Documentation. Template Customization Guide

How to Run an Apache HTTP Server With a Protocol

InternetVista Web scenario documentation

Portals and Hosted Files

PHP. Introduction. Significance. Discussion I. What Is PHP?

DEERFIELD.COM. DNS2Go Update API. DNS2Go Update API

XHTML BASICS. Institute of Finance Management CIT Department Herman Mandari

PHP Tutorial From beginner to master

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

A Simple Shopping Cart using CGI

Introduction to Programming and Computing for Scientists

Payment Page Integration

Chapter 2: Interactive Web Applications

APACHE WEB SERVER. Andri Mirzal, PhD N

1. Stem. Configuration and Use of Stem

Linklok URL TM V2.90

End User Guide The guide for /ftp account owner

OpenIMS 4.2. Document Management Server. User manual

Protocolo HTTP. Web and HTTP. HTTP overview. HTTP overview

Inserting the Form Field In Dreamweaver 4, open a new or existing page. From the Insert menu choose Form.

PHP Authentication Schemes

How to Make a Working Contact Form for your Website in Dreamweaver CS3

Dynamic Web-Enabled Data Collection

Ipswitch Client Installation Guide

MPI Frequently Asked Questions

Windows Hosting Documentation

Slides from INF3331 lectures - web programming in Python

FORMS. Introduction. Form Basics

10. Java Servelet. Introduction

1Intro. Apache is an open source HTTP web server for Unix, Apache

Transcription:

Perl/CGI

CGI Common: Gateway: Programming in Perl Interface: interacts with many different OSs CGI: server programsprovides uses a well-defined users with a method way to to gain interact access with to different a Web the standard programming interface between Web servers and external programs CGI standard lets Web browsers pass information to programs written in any language 67

Writing and Running CGI Scripts CGI scripts can be compiled programs or batch files or any executable entity Typically CGI scripts are written in: Perl scripts C/C++ programs Unix Scripts The 2 most common ways of running a CGI script are: From an HTML form the action attribute of the form specifies the CGI script to be run Direct URL reference A CGI script can be run directly by giving the URL explicitly in HTML 68

Why Use Perl for CGI? Perl is the standard for CGI programming: Socket support create programs that interface seamlessly with Internet protocols Pattern matching ideal for handling form data and searching text Flexible text handling no details to worry Advantage of an interpreted language simplicity in development, debugging, and revision 69

How Does CGI Work? CGI programs are always placed on a disk that the Web server has access to Web servers are generally configured so that all CGI applications are placed into a cgi-bin directory 70

There We browser can hypertext are many ways to invoke CGI programs besides using a web <a start to href= cgi-user/ftang/www/cgi-bin/hello.cgi >Click visit CGI link the programs URL from: here to run the hello CGI We a program</a> For can button server-side example, use on a question a include passing HTML form keywords markto pass that will information be used to a search a CGI program. Generally <a The Wine</a> href= cgi-user/ftang/www/cgi-bin/search.pl?wine+1993 > information follows the question mark will be available to your Search CGI for program 1993 A type CS 299 hypertext in the through speaking, URL the QUERY_STRINGenvironment a visitors CGI program. to your web site variables. should never have to Web Programming link should and always Designbe provided to start the program. Invoke CGI Programs 71

The Hello World Example When the web server executes your CGI program, it automatically opens the STDIN, STDOUT, STDERR file handles for you. STDIN: the standard input of your CGI program might contain information that was generated by an HTML form. Otherwise, you shouldn t use STDIN. STDOUT: the standard output of your CGI program is linked to the STDIN of the web browser. STDERR: The standard output of your CGI program is linked to the web server s log file. 72

CGI Script Output A CGI script must send information back in the following format: The output header A blank line The output data For examples: print( Content Type: text/html\n\n ); CS Note: 299 Web Between Programming the and Header Design and Data there MUST be a blank line 73

Output Header: Content-Type text/html, The output postscript, text/plain, header video/mpeg image/gif, image/jpeg, application/, for a CGI script must specify an output type 3 forms of Header Type: Content-Type: Location Status Content-Type is the most popular type See our hello world example: http://www.csupomona.edu/cgi-user/ftang/www/cgibin/hello.pl 74

Output Header: Location The Location header is used to redirect the client Web browser to another page For example: print "Location: http://www.google.com/\n\n"; 75

CGI Script Input A CGI script will often require some form of input in order to operate We ll study: What form of input a CGI can receive How a CGI receives input How to process the input in a CGI Perl script How a useful Perl library makes this easy 76

Accepting Input from the Browser A CGI script can receive data in many ways: Environment variables Standard input Data can be passed as standard input through the POST method of an HTML form Arguments of CGI script If you call a CGI script directly or use the GET method of a form, arguments are following the? after the script URL and multiple arguments are separated by & 77

Receiving/Processing Information in CGI Two basic ways: Do it yourself: write Perl code to process the input Use pre-written Perl libraries http://stein.cshl.org/www/cgi/ a Perl5 CGI Library 78

Forms and CGI: What Can They Do? A CGI program can be used to: accept the data which the user inputs do something with it email the data to someone add an entry to a database write out a text file create a customized display anything that you can program send a reply back to user 79

Form Processing The two most important options with the <form> tag: method: specifies the manner in which form information is passed to the CGI scripts: POST: contacts the CGI program and once connection established sends data GET: Contacts the CGI program and sends data in a single transaction action: specifies the URL of the CGI script that will CS 299 be Web invoked Programming when and the Design submit button is clicked <from method = post action= /cgi-bin/hello.pl > 80

Handling Form Information The GET method <form http://somserver/cgi-bin/foo.pl?fname=craig&lname=kelley method= get action= /cgi-bin/guestbook.pl > The GET method appends all of the form data to the end of the URL used to invoke the CGI script. A question mark is used to separate the original URL and the form information. The GET method can t be used for larger forms 81

Handling Form Information The POST method <form method= post action= cgi-bin/guestbook.pl > The POST method sends all the form information to the CGI program using the STDIN file handle. Which to use? It is good practice to use the GET method whenever you are able to because the POST method is more difficult for a user to manage, and it doesn t function well with a browser s back or history button. It is good to use the POST method when something secure or private is being sent such as password or a credit card information. 82

CGI.pm: a Perl 5 CGI Library This Perl 5 library uses objects to do many things. It Uses objects to create Web fill-out forms on the fly and to parse their contents Provides a simple interface for parsing and interpreting query strings passed to CGI scripts Offers a rich set of functions for creating fill-out forms Everything is done through a CGI object The most basic use of CGI.pm is to get the query parameters submitted to your script. To do so, put the CS following 299 Web Programming at the top and of Design your Perl/CGI programs: Use CGI; $query = new CGI; Or Use CGI qw(:standard); http://stein.cshl.org/www/cgi/ 83

Function Oriented vs. Object Oriented #!/usr/local/bin/perl #!/usr/local/bin/perl # imports standard set of functions use CGI qw/:standard/; use CGI; $q = new CGI; print header(), print $q->header(), start_html(-title=>'wow!'), $q->start_html(-title=>'wow!'), h1('wow!'), $q->h1('wow!'), 'Look Ma, no hands!', end_html(); 'Look Ma, no hands!', $q->end_html(); http://stein.cshl.org/www/cgi/#functionvsoo 84

What Can You Do with the Query Object? Fetch the names of all the parameters passed to your script @names = $query->param; Fetch the value(s) of a named parameter @values = $query->param( foo ); # or CS http://www.csupomona.edu/~ftang/www/courses/cs299-s09/examples/simple.html And many other methods 299 Web Programming and Design $value = $query->param( foo ); Example: 85

Printing HTML Instead of printing the quote marks and write in new line characters An easier way is to use a special print command in Perl: print <<AnyWord AnyWord #!/usr/bin/perl print "Content-type: text/html\n\n"; print <<ENDHTML; <html> <head> <title>cgi Test</title> </head> <body> <a href="http://what.com">click Here</a> </body> </html> ENDHTML ; 86

Save the Current State of a Form Save the state to a file $query->save(filehandle); The contents are written out as TAG=VALUE pairs Save the state in a self-referencing URL $my_url = $query->self_url; Useful when you want to jump around within a script-generated document using internal anchors, but don t want to disrupt the current contents. 87

Three Different Methods Print out the information after form submission www.csupomona.edu/~ftang/www/cgi-bin/procform1.cgi Submit the information to an email address www.csupomona.edu/~ftang/www/cgi-bin/procform2.cgi Save the information to a file (will not use this one) www.csupomona.edu/~ftang/www/cgi-bin/procform3.cgi To run the above programs: http://www.csupomona.edu/~ftang/www/courses/cs299- S09/examples/runcgi.html 88

Creating the HTTP Header Creating the standard header print $query->header() default to text/html Creating the header for a redirection request print $query->redirect( http://www.google.com ); 89

HTML Shortcuts Create an HTML header print $query->start_html(many options); End an HTML document print $query->end_html; Shortcut methods for many HTML tags Importing CGI methods Single query object import CGI module methods use CGI qw(:standard); $dinner = param( entrée ); 90

Other http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/style.pl Supports http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/bio.pl Support for CSS http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/contact.pl check http://stein.cshl.org/www/cgi/ for details Support for JavaScript Similar as CSS 91

Many Perl/CGI Examples http://stein.cshl.org/www/cgi/examples/ 92