1 / 17

CGI Programming: Part 1

CGI Programming: Part 1. 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. What is CGI? (cont).

ulla
Télécharger la présentation

CGI Programming: Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 taxes. • 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 script’s 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 (<a>) tag. • For example, <a href = “http://www.foo.com/cgi-bin/bar.pl”> DO IT </a>

  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 form’s values are sent to the server as a string. • Represented as a sequence of name=value pairs: • name is the widget’s name. • value is the widget’s 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 form’s values to a CGI program. • Get is the default method. • Data string is attached to URL. • Seen in browser’s 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 • <form> tags are used to create a form. • For example, <form method=“post” action=“file.pl”> … <input type = “submit” value = “submit form”> </form>

More Related