📄 ch19.htm
字号:
environment variables.<P>Using either of these approaches will let you create <I>canned</I>CGI requests. By creating these requests ahead of time, you canreduce the amount of typing errors that your users might otherwisehave. Later in this chapter, the "CGI and Environment Variables"section discusses all of the environment variables you can useinside CGI programs.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>Generally speaking, visitors to your Web site should never have to type in the URL for a CGI program. A hypertext link should always be provided to start the program.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="YourFirstCGIProgram"><FONT SIZE=5 COLOR=#FF0000>Your First CGI Program</FONT></A></H2><P>You can use any text editor or word processor in the world tocreate your CGI programs because they are simply Perl programsthat are invoked by a URL instead of the command line. Listing19.1 contains a five-line CGI program-nearly the smallest programyou can have.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Turn on the warning option.<BR><I>Turn on the strict pragma.<BR><I>Send the HTTP header to the Web browser.<BR><I>Send a line of text to the Web browser.</I></I></I></I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 19.1 19LST01.PL-A Very Small CGI Program<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>#!/usr/local/bin/perl -wuse strict;print "Content-type: text/plain\n\n";print "Hello, World.\n";</PRE></BLOCKQUOTE><HR><P>The file that contains this CGI program should be placed in yourWeb server's <TT>cgi-bin</TT> directory.Then, the URL for this program will be something like <TT>http://localhost/cgi-bin/test.pl</TT>(change localhost to correspond to your Web server's hostname).Enter this URL into your Web browser and it should display a Webpage saying "This is a test."<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>You may wonder how the Web server knows that a CGI program should be executed instead of being displayed. This is an excellent question. It can be best answered by referring to the documentation that came with your particular server.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>When the Web server executes your CGI program, it automaticallyopens the <TT>STDIN</TT>, <TT>STDOUT</TT>,and <TT>STDERR </TT>file handles foryou.<UL><LI><TT><B><FONT FACE="Courier">STDIN</FONT></B></TT>-The standardinput of your CGI program might contain information that was generatedby an HTML form. Otherwise, you shouldn't use STDIN. See "Inputsto Your CGI Program" later in this chapter for more information.<LI><TT><B><FONT FACE="Courier">STDOUT</FONT></B></TT>-The standardoutput of your CGI program is linked to the <TT>STDIN</TT>of the Web browser. This means that when you print informationusing the <TT>print()</TT> fuNCtion,you are essentially writing directly to the Web browser's window.This link will be discussed further in the "HTTP Headers"section later in this chapter.<LI><TT><B><FONT FACE="Courier">STDERR</FONT></B></TT>-The standardoutput of your CGI program is linked to the Web server's log file.This is very useful when you are debugging your program. Any outputfrom the <TT>die()</TT> or <TT>warn()</TT>fuNCtion will be placed into the server's log file. The STDERRfile handle is discussed further in the "Debugging CGI Programs"section later in this chapter.</UL><P>The Web server will also make some information available to yourCGI program through environment variables. You may recall the<TT>%ENV</TT> hash from <A HREF="ch12.htm" >Chapter 12</A>,"Using Special Variables." Details about the environmentvariables you can use can be found in the "Environment Variables"section later in this chapter.<H2><A NAME="WhyAreFilePermissionsImportantinUNIX"><FONT SIZE=5 COLOR=#FF0000>Why Are File Permissions Important in UNIX?</FONT></A></H2><P>File permission controls can access files in UNIX systems. Quiteoften, I hear of beginning CGI programmers who try to write filesinto a directory in which they do not have write permission. UNIXpermissions are also called <I>rights</I>.<P>UNIX can control file access in a number of ways. There are threelevels of permissions for three classes of users. To view thepermissions on a file use the <TT>ls<I> </I></TT>commandwith the <TT>-l</TT> command-lineoption. For example:<BLOCKQUOTE><PRE>C:indyunix:~/public_html/pfind><B>ls -l</B>total 40-rw-r--r-- 1 dbewley staff 139 Jun 18 14:14 home.html-rwxr-xr-x 1 dbewley staff 9145 Aug 14 07:06 pfinddrwxr-xr-- 2 dbewley staff 512 Aug 15 07:11 tmp</PRE></BLOCKQUOTE><P>Each line of this listing indicates a separate directory entry.The first character of the first column is normally either a dashor the letter <TT>d</TT>. If a directoryentry has a <TT>d</TT>, it means thatthe entry is a subdirectory of the current directory.<P>The other nine characters are the file permissions. Permissionsshould be thought of in groups of three, for the three classesof user. The three classes of user are:<UL><LI><B>user</B>-the owner of the file or directory. The owner'sname is displayed in the third column of the <TT>ls</TT>command's output.<LI><B>group</B>-the group that owns the file. Files can haveboth individual owners and a group. Several users can belong toa single group.<LI><B>others</B>-any user that is not the owner or in the groupthat owns the file.</UL><P>Each of the classes can have one or more of the following threelevels of permission:<UL><LI><B>r</B>-the class can read the file or directory.<LI><B>w</B>-the class can write to the file or directory.<LI><B>x</B>-the class can execute the file or list the directory.</UL><P>If a permission is not allowed to the user that ran the <TT>ls</TT>command, its position is filled with a dash. For example:<BLOCKQUOTE><PRE><B>ls -l hform.html</FONT></B><FONT SIZE=2 FACE="Courier">-rwx------ 1 dbewley staff 11816 May 9 09:19 slideshow.pl</PRE></BLOCKQUOTE><P>The owner, <TT>dbewley</TT>, has fullrights - read, write, and execute for this file. The group, staff,and everyone else have no rights.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>Perl scripts are not compiled; they must be read by the Perl interpreter each time they are run. Therefore, Perl scripts, unlike compiled programs, must have execute <FONT FACE="LI Helvetica Light Oblique">and</FONT> read permissions.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Here is another example:<BLOCKQUOTE><PRE><B>ls -l pfind.pl</FONT></B><FONT SIZE=2 FACE="Courier">-rwxr-x-- 1 dbewley staff 2863 Oct 10 1995 pfind.pl</PRE></BLOCKQUOTE><P>This time, the owner has full access while the group staff canread and execute the file. All others have no rights to this file.<P>Most HTML files will have permissions that look like this:<BLOCKQUOTE><PRE><B>ls -l schedule.html</FONT></B><FONT SIZE=2 FACE="Courier">-rw-r--r-- 1 dbewley staff 2439 Feb 8 1996 schedule.html</PRE></BLOCKQUOTE><P>Everyone can read it, but only the user can modify or delete it.There is no need to have execute permission siNCe HTML is notan executable language.<P>You can change the permissions on a file by using the <TT>chmod</TT>command. The <TT>chmod</TT> commandrecognizes the three classes of user as <TT>u</TT>,<TT>g</TT>, and <TT>o</TT>and the three levels of permissions as <TT>r</TT>,<TT>w</TT>, and <TT>x</TT>.It grants and revokes permissions with a <TT>+</TT>or <TT>-</TT> in conjuNCtion witheach permission that you want to change. It also will accept an<TT>a</TT> for all three classes ofusers at oNCe.<P>The syntax of the <TT>chmod</TT> commandis:<BLOCKQUOTE><PRE>chmod <options> <file></PRE></BLOCKQUOTE><P>Here are some examples of the <TT>chmod</TT>command in action<BLOCKQUOTE><PRE><B>ls -l pfind.pl</FONT></B><FONT SIZE=2 FACE="Courier">-rw------ 1 dbewley staff 2863 Oct 10 1995 pfind.pl<B>chmod u+x pfind.plls -l pfind.pl</B>-rwx------ 1 dbewley staff 2863 Oct 10 1995 pfind.pl</PRE></BLOCKQUOTE><P>The first <TT>ls</TT> command showsyou the original file permissions. Then, the <TT>chmod</TT>command add execute permission for the owner (or user) of <TT>pfind.pl</TT>.The second <TT>ls</TT> command displaysthe newly changed permissions.<P>To add these permissions for both the group and other classes,use <TT>go+rx</TT> as in the followingexample. Remember, users must have at least read and execute permissionsto run Perl scripts.<BLOCKQUOTE><PRE><B>ls -l pfind.pl</FONT></B><FONT SIZE=2 FACE="Courier">-rwx------ 1 dbewley staff 2863 Oct 10 1995 pfind.pl<B>chmod go+rx pfind.plls -l pfind.pl</B>-rwxr-xr-x 1 dbewley staff 2863 Oct 10 1995 pfind.pl</PRE></BLOCKQUOTE><P>Now, any user can read and execute <TT>pfind.pl</TT>.Let's say a serious bug was found in pfind.pl and we don't wantit to be executed by anyone. To revoke execute permission forall classes of users, use the <TT>a-x</TT>option with the <TT>chmod</TT> command.<BLOCKQUOTE><PRE><B>ls -l pfind.pl</FONT></B><FONT SIZE=2 FACE="Courier">-rwxr-xr-x 1 dbewley staff 2863 Oct 10 1995 pfind.pl<B>chmod a-x pfind.plls -l pfind.pl</B>-rw-r--r-- 1 dbewley staff 2863 Oct 10 1995 pfind.pl</PRE></BLOCKQUOTE><P>Now, all users can read <TT>pfind.pl</TT>,but no one can execute it.<H2><A NAME="HTTPHeaders"><FONT SIZE=5 COLOR=#FF0000>HTTP Headers</FONT></A></H2><P>The first line of output for most CGI programs must be an HTTPheader that tells the client Web browser what type of output itis sending back via <TT>STDOUT</TT>.Only scripts that are called from a server-side iNClude are exemptfrom this requirement.<BR><P><CENTER><B>Table 19.1 A List of HTTP Headers</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=166><I>Response Type</I></TD><TD WIDTH=238><I>HTTP Header</I></TD></TR><TR><TD WIDTH=166>Text</TD><TD WIDTH=238>Content Type: text/plain</TD></TR><TR><TD WIDTH=166>HTML page</TD><TD WIDTH=238>Content Type: text/html</TD></TR><TR><TD WIDTH=166>Gif graphic</TD><TD WIDTH=238>Content Type: image/gif</TD></TR><TR><TD WIDTH=166>Redirection to another Web page</TD><TD WIDTH=238>Location: http://www.foobar.com</TD></TR><TR><TD WIDTH=166>Cookie</TD><TD WIDTH=238>Set-cookie: ...</TD></TR><TR><TD WIDTH=166>Error Message</TD><TD WIDTH=238>Status: 402 </TD></TR></TABLE></CENTER><P><P>All HTTP headers must be followed by a blank line. Use the followingline of code as a template:<BLOCKQUOTE><PRE>print("Content Type: text/html\n\n");</PRE></BLOCKQUOTE><P>Notice that the HTTP header is followed by <I>two</I> newlinecharacters. This is very important. It ensures that a blank linewill always follow the HTTP header.<P>If you have installed any helper applications for Netscape orare familiar with MIME types, you already recognize the <TT>text/plain</TT>and <TT>text/html</TT> parts of the<TT>Content Type</TT> header. Theytell the remote Web browser what type of information you are sending.The two most common MIME types to use are <TT>text/plain</TT>and <TT>text/html</TT>.<P>The <TT>Location</TT> header is usedto redirect the client Web browser to another Web page. For example,let's say that your CGI script is designed to randomly choosefrom among 10 different URLs in order to determine the next Webpage to display. ONCe the new Web page is chosen, your programoutputs it like this:<BLOCKQUOTE><PRE>print("Location: $nextPage\n\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -