HELP! * GREY=local Full HTML
for
CGI 编程简介
Given by Nancy J. McCracken at ECS400 Senior Undergraduate Course
on Spring Semester 1996. Foils prepared 2 June 1996
摘要
* 内容索引
See also color IMAGE
- CGI 指 Common Gateway Interface (公共网关接口) , 它是其它程序和系统使用和
HTTP 客户和服务器同样的协议到 HTTP Web 协议的接口.
- 在这一部分将包括的内容有:
- 从 web 页向 CGI 描述传送信息
- 在服务器一方处理信息并向 web 客户返回格式化的 web 页
- 一个使用 Perl 作为描述语言的例子
- 其他 CGI 功能的简单描述
- 参考文献:
- HTML and CGI Unleashed, John December and Mark GInsburg, chapters
19, 20 and 21, Sams.net Publishing.
- CGI Programming on the World Wide Web, Gundavaram, O'Reilly &
Associates.
- The CGI Book, William Weinman, New Riders Publishing.
- Web documents.
CGI 编程的全部内容索引
1 CGI 编程
2 CGI 编程
3 客户,服务器和CGI描述之间的数据流
4 客户,服务器和CGI描述之间的数据流
5 form举例: Hello, World!
6 用Perl语言编写的 CGI 程序举例: Hello,
World!
7 通过环境变量向 CGI 程序传递数据
8 另一种通过环境变量传递数据的方式
9 环境变量的其他信息
10 作为标准输入向 CGI 程序传递数据
11 用来从web form 中读取输入的 Perl 子程序
- 第一部分
12 Perl 子程序 - 第二部分
13 CGI 程序输出:对 web 服务器的响应
14 一些 CGI 编程的实用技巧
15 其他实用技巧
目录
摘要
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 1 CGI 编程
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
全部文件目录
- Nancy McCracken
- ECS 400, Software Technologies for the WWW
- 3-234 Center for Science and Technology
- Syracuse University
- 111 College Place
- Syracuse NY 13244-4100
- February 26, 1996
- Click here for body text
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 2 CGI 编程
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
全部文件目录
- CGI 指 Common Gateway Interface (公共网关接口) , 它是其它程序和系统使用和
HTTP 客户和服务器同样的协议到 HTTP Web 协议的接口.
- 在这一部分将包括的内容有:
- 从 web 页向 CGI 描述传送信息
- 在服务器一方处理信息并向 web 客户返回格式化的 web 页
- 一个使用 Perl 作为描述语言的例子
- 其他 CGI 功能的简单描述
- 参考文献:
- HTML and CGI Unleashed, John December and Mark GInsburg, chapters
19, 20 and 21, Sams.net Publishing.
- CGI Programming on the World Wide Web, Gundavaram, O'Reilly &
Associates.
- The CGI Book, William Weinman, New Riders Publishing.
- Web documents.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 5 Example form for Hello, World!
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * Critical
Information in IMAGE
Full
HTML Index
- This example consists of a simple form with just a submit button
to activate the CGI program. Note that no data is being sent from the form
to the CGI program in this simple example.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 6 Example CGI program in Perl for Hello, World!
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * Critical
Information in IMAGE
Full
HTML Index
- The Perl program returns output which is properly formatted HTML.
The server returns it to the browser, which displays it as a page.
- Returning the html output is pretty simple as the server and browser
handle the encoding and decoding of the MIME formatted message. The complications
arise from sending text from the form to the CGI program; there are several
ways to do it and t he CGI program must decode the message.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 7 Pass Data to a CGI Program through Environment Variables
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
Full
HTML Index
- Two environment variables QUERY_STRING and PATH_INFO are used to
pass data to the CGI program - there are several ways to do this.
- Using a normal HTML link to pass data:
- Everything after the first question mark in a URL is put into the
QUERY_STRING variable:
- <a href="http://www.some.box/sign.pl?passed-argument">
Click here to run the program. </a>
- The QUERY_STRING variable is "passed-argument".
- Everything after an executable in the path name is put into the
PATH_INFO variable:
- <a href="http://www.some.box/walk.pl/direction=north/speed=slow">
Start the program. </a>
- The PATH_INFO variable is "direction=north/speed=slow".
- These techniques are required by some search engines, such as WAIS,
to pass keywords for the search.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 8 Another way to pass data through environment variables
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
Full
HTML Index
- Using the method "get" in a form to pass data, all the
input that the user types is put in the QUERY_STRING variable:
- <form method=get action="http://www.some.box/name.pl">
- Type your first name <input name="First Name"><br>
- Type your last name <input name="Last Name"><br>
- <input type=submit value="submit">
- </form>
- If the user types "Winona" and "Ryder" as values,
the QUERY_STRING environment variable would have the encoded value "First+Name=Winona&Last+Name=Ryder".
- Here is a Perl program that would print that string on the web client's
window (without regular html tags):
- #!/usr/local/bin/perl
- print "Content-type: text/html\n\n";
- print "You typed \"$ENV{QUERY_STRING}\" in the input
boxes\n";
- Method=Get is NOT RECOMMENDED as input too long can be lost!
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 9 Other Information in environment variables
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * Critical
Information in IMAGE
Full
HTML Index
- The web server also makes available information about the user and
the server, including such things as what type of browser made the request.
- A list of environment variables available to the CGI program:
- GATEWAY_INTERFACE REMOTE_HOST
- SERVER_NAME REMOTE_ADDR
- SERVER_SOFTWARE AUTH_TYPE
- SERVER_PROTOCOL REMOTE_USER
- SERVER_PORT REMOTE_IDENT
- REQUEST_METHOD CONTENT_TYPE
- PATH_INFO CONTENT_LENGTH
- PATH_TRANSLATED HTTP_FROM
- SCRIPT_NAME HTTP_ACCEPT
- DOCUMENT_ROOT HTTP_USER_AGENT (browser)
- QUERY_STRING HTTP_REFERER
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 10 Passing data as Standard Input to the CGI program
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
Full
HTML Index
- It is recommended to use a form with METHOD=POST to safely pass
any amount of data through STDIN.
- The CONTENT_LENGTH environment variable is set to the number of
characters being sent, and the CONTENT_TYPE variable is set to "application/x-www-form-urlencoded".
- The data is encoded by the server:
- The fields are separated by the unencoded &.
- Within each field, an unencoded = separates the fieldname input
form and the data.
- Spaces within a field are translated to +.
- Certain other keyboard characters are encoded to %[hex equivalent]
- for example, ! becomes %3D.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 11 Perl subprogram to read input from web forms - Part I
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * Critical
Information in IMAGE
Full
HTML Index
- This subroutine works with either the GET or POST method, obtaining
the user input string from the form into a scalar variable "$in".
It then splits this string into fields into the array "@in",
where each element contains the encoded string for one field.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 12 Perl subprogram - Part II
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * Critical
Information in IMAGE
Full
HTML Index
- For each field string, the subroutine converts all the encoding
symbols. It then creates an associative array "%in" with a keyword,value
pair from each field of the web form.
- This subroutine can be used without change in any Perl CGI program,
unless you wish to have checkboxes on the form which may return the same
name with more than one value.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 13 CGI Program Output: the response to the web server
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
Full
HTML Index
- All output written by the CGI program to STDOUT is taken by the
server to process. The output should start with a header in one of three
types:
- Location: the server sends another file to the client (and terminates
connection).
- print "Location: http://www.some.box.com/the_other_file.html";
- Status: The server will return a status message to the client (and
terminates connection).
- print "Status: 305 Document moved\n";
- Content-type: The server sends all remaining output to the client
(after the mandatory blank line!), terminating only when the script does.
- print "Content-type: text/html\n\n";
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 14 Some CGI programming practical tips
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * See
also color IMAGE
Full
HTML Index
- On the web server that you are doing CGI programming, put the HTML
pages with forms in a directory somewhere under the server's "document
root" and the CGI program somewhere under the server's "cgi bin".
The CGI program must ha ve permissions properly set to be executed by the
server. Furthermore, if the CGI program reads or writes to other files,
then the server must have permission to do so.
- You can first debug your Perl program by executing it directly in
the cgi-bin directory and providing test input in a file
- prog.pl < input.data
- When a CGI program crashes, an error should show up in the server's
error_log file.
HELP! * GREY=local
HTML version of LOCAL Foils prepared 2 June 1996
Foil 15 More practical tips
From Introduction to CGI Programming ECS400 Senior Undergraduate
Course -- Spring Semester 1996. * Critical
Information in IMAGE
Full
HTML Index
- Your Perl script is run with the current directory as the cgi-bin
directory in which it resides, so in any file systems accessess that you
program in Perl, the path names are evaluated accordingly. Suppose that
you have the file system structure i n the example below, then to open
file1.txt or file2.txt from prog.pl, use:
- open (FILE1, "file1.txt"); and open (FILE2, "../../htdoc/njm/file2.txt");
Northeast
Parallel Architectures Center, Syracuse University, npac@npac.syr.edu
If you have any comments about this server, send e-mail to webmaster@npac.
syr.edu.
Page produced by wwwfoil on Mon Sep 16
1996 ---------- Xiaoming Li (315)443-3890 (o) NPACT (315)443-1973 (f)