CGI Programming: Part 1 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CGI Programming: Part 1

Description:

A form is a collection of widgets in a web page. Solicit responses from the user. ... A string representation of the widgets' values is sent to the server. Form Values ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 18
Provided by: csUt8
Category:
Tags: cgi | cgi | create | page | part | programming | web | widgets

less

Transcript and Presenter's Notes

Title: CGI Programming: Part 1


1
CGI Programming Part 1
2
What is CGI?
  • CGI Common Gateway Interface
  • Provides a standardized way for web browsers to
  • Call programs on a server.
  • Pass data to programs on a server.
  • Receive responses from programs on a server.

3
What is CGI? (cont)
  • CGI is the interface between server programs and
    other software.
  • CGI is not a Perl specific concept.
  • Any language can produce CGI programs.
  • Why Perl?
  • Perl provides a nice interface for creating CGI
    scripts.

4
How Does CGI Work?
  • Phase 1 Create.
  • Phase 2 Request/Execute.
  • Phase 3 Respond/Display.

5
Phase 1
  • A CGI script is created.
  • e.g. a Perl program to do your tasks.
  • The script is placed on a server.
  • Made executable.
  • Given appropriate permissions.
  • A webpage is created and linked to the CGI
    script.
  • Webpage is the scripts interface to the world.

6
Phase 2
  • A person visits the webpage and submits a request
    to run the script.
  • Browser contacts the server with the CGI script
  • Asks to run the script.
  • Passes input parameters to the script.
  • Server runs the script on the input parameters.

7
Phase 3
  • Script produces output in the form of HTML.
  • Server takes the output and returns it to the web
    browser.
  • Browser displays the output as an HTML page.
  • Page may reference other CGI scripts.

8
An Overview of the Process
Request
CGI Program
Input/ Output
Server
HTML
9
Calling a CGI Program
  • CGI programs are called from HTML files.
  • A common/simple way is with the HREF attribute of
    the anchor (ltagt) tag.
  • For example,
  • lta href http//www.foo.com/cgi-bin/bar.plgt
  • DO IT
  • lt/agt

10
Returning the Output
  • CGI programs must create HTML output to return to
    the client.
  • HTML is the common language between the client
    and the CGI program.
  • Communication with client is through standard
    output.
  • Output goes to server and then goes to screen.
  • Use print function to send HTML output to client.

11
HTML OUTPUT
  • First line of HTML output must specify the type
  • i.e. type of the content of the output.
  • The type is usually text/html.
  • For example,
  • print Content-type text/html\n\n
  • There must be a blank line after the first line.

12
A Simple Example
13
More Powerful Interaction
  • Many webpages gather information from their
    visitors.
  • A more powerful interaction between CGI program
    and browser is needed.
  • This interaction is provided by forms.

14
Forms
  • A form is a collection of widgets in a web page.
  • Solicit responses from the user.
  • A form must include a submit button!
  • When the submit button is clicked
  • The CGI program specified in the form is
    contacted.
  • A string representation of the widgets values is
    sent to the server.

15
Form Values
  • Remember a forms values are sent to the server
    as a string.
  • Represented as a sequence of namevalue pairs
  • name is the widgets name.
  • value is the widgets value.
  • Values are textual only!
  • Other data types cannot be sent.
  • Validity of values must be checked by CGI program.

16
Get vs. Post
  • There are two ways to send a forms values to a
    CGI program.
  • Get is the default method.
  • Data string is attached to URL.
  • Seen in browsers address bar.
  • Server removes string from URL.
  • Stored in environment variable, QUERY_STRING.
  • Post is the optional method.
  • Data string is sent through standard input.
  • CGI program can simply read it.
  • Length of string is stored in environment
    variable, CONTENT_LENGTH.

17
Creating a Form
  • ltformgt tags are used to create a form.
  • For example,
  • ltform methodpost actionfile.plgt
  • ltinput type submit value submit formgt
  • lt/formgt
Write a Comment
User Comments (0)
About PowerShow.com