home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _cc55de5722256c3565c6c6ebaff7e669 < prev    next >
Text File  |  2004-06-01  |  15KB  |  295 lines

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3.  
  4. <head>
  5. <title>ActivePerl FAQ - Web Programming</title>
  6. <link rel="STYLESHEET" href="../../Active.css" type="text/css" media="screen">
  7. </head>
  8.  
  9. <body>
  10.  
  11. <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAE2BB">
  12. <tr> 
  13. <td width="57"><a target=_blank href="http://www.ActiveState.com/ActivePerl/">
  14. <img src="../../images/activeperl_logo.gif" width="57" height="48" border="0" alt="ActivePerl"></a></td>
  15. <td><div align="center" class="heading">ActivePerl User Guide</div></td>
  16. <td width="112"><a target=_blank  href="http://www.ActiveState.com">
  17. <img src="../../images/AS_logo.gif" width="112" height="48" border="0"  alt="ActiveState" /></a></td>
  18. </tr>
  19. <tr>
  20. <td class="lineColour" colspan="3"></td>
  21. </tr>
  22. </table>
  23.  
  24. <h1>Web Programming (CGI and PerlIS)</h1>
  25.  
  26. <ul>
  27.   <li><a class="doc" href="#NAME">NAME</a></li>
  28.   <li><a class="doc" href="#DESCRIPTION">DESCRIPTION</a>
  29.     <ul>
  30.       <li><a class="doc" href="#What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I
  31.         get more information on it?</a></li>
  32.       <li><a class="doc" href="#What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I
  33.         get more information on it?</a></li>
  34.       <li><a class="doc" href="#How_do_I_return_a_graphics_file_">How do I return a graphics
  35.         file from a CGI script?</a></li>
  36.       <li><a class="doc" href="#My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't seem
  37.         to run right under PerlIS.</a></li>
  38.       <li><a class="doc" href="#How_does_my_script_know_if_it_s_">How does my script know if
  39.         it's running under ActivePerl or</a></li>
  40.       <li><a class="doc" href="#What_CGI_modules_run_with_Perl_f">What CGI modules run with
  41.         ActivePerl?</a></li>
  42.       <li><a class="doc" href="#How_do_I_use_redirection_in_my_s">How do I use redirection
  43.         in my script?</a></li>
  44.       <li><a class="doc" href="#What_are_cookies_and_how_do_I_us">What are cookies and how
  45.         do I use them?</a></li>
  46.       <li><a class="doc" href="#How_do_I_get_the_e_mail_address_">How do I get the e-mail
  47.         address of the user?</a></li>
  48.       <li><a class="doc" href="#I_need_a_CGI_script_to_do_a_cert">I need a CGI script to do
  49.         a certain task. Has anyone done it before?</a></li>
  50.       <li><a class="doc" href="#How_do_I_test_my_CGI_programs_th">How do I test my CGI
  51.         programs that take arguments without running</a></li>
  52.       <li><a class="doc" href="#The_Content_Type_header_field_I">The Content-Type: header
  53.         field I output from my CGI script shows up</a></li>
  54.       <li><a class="doc" href="#When_I_try_to_run_a_CGI_script_f">When I try to run a CGI
  55.         script from my browser, it tries to</a></li>
  56.       <!-- <li><a class="doc" href="#What_s_Perl_for_WSAPI_">What's Perl for WSAPI?</a></li> -->
  57.     </ul>
  58.   </li>
  59.   <li><a class="doc" href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</a></li>
  60. </ul>
  61.  
  62. <h2><a name="NAME">NAME</a></h2>
  63. <p>ActivePerl-faq7 - Web Programming (CGI and PerlIS)</p>
  64.  
  65. <h2><a name="DESCRIPTION">DESCRIPTION</a></h2>
  66. <p>Web development with ActivePerl</p>
  67.  
  68. <h2><a name="What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I get
  69. more information on it?</a></h2>
  70. <p>HTTP (HyperText Transfer Protocol) is the protocol that web browsers use to
  71. talk to web servers. The official specification for the HTTP standard is
  72. available on the W3 Consortium web server at:</p>
  73. <pre>
  74.     <a class="doc" href="http://www.w3.org/pub/WWW/Protocols/">http://www.w3.org/pub/WWW/Protocols/</a>
  75. </pre>
  76. <p>Some more readable introductions can be found on the Yahoo HTTP page at:</p>
  77. <pre>
  78.     <a class="doc" href="http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/HTTP/">http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/HTTP/</a>
  79. </pre>
  80.  
  81. <h2><a name="What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I get more
  82. information on it?</a></h2>
  83. <p>CGI (Common Gateway Interface) is a protocol used by web servers to run
  84. server programs. Scripts that support the CGI protocol are sometimes called
  85. ``CGI scripts''; this leads to the unfortunate misperception that CGI is a
  86. language of its own.</p>
  87. <p>The classic information on CGI is available on the NCSA server at:</p>
  88. <pre>
  89.     <a class="doc" href="http://hoohoo.ncsa.uiuc.edu/cgi/">http://hoohoo.ncsa.uiuc.edu/cgi/</a>
  90. </pre>
  91. <p>If you haven't read this, read it now. If it doesn't click for you, you can
  92. check the Yahoo CGI page at:</p>
  93. <pre>
  94.     <a class="doc" href="http://dir.yahoo.com/Computers_and_Internet/Software/Internet/World_Wide_Web/Servers/Server_Side_Scripting/Common_Gateway_Interface__CGI_/">http://dir.yahoo.com/Computers_and_Internet/Software/Internet/World_Wide_Web/Servers/Server_Side_Scripting/Common_Gateway_Interface__CGI_/</a>
  95. </pre>
  96. <p>(All one URL)</p>
  97. <p>Try also the following URL for CGI programming in Perl:</p>
  98. <pre>
  99.     <a class="doc" href="http://www.perl.com/CPAN-local/doc/FAQs/cgi/perl-cgi-faq.html">http://www.perl.com/CPAN-local/doc/FAQs/cgi/perl-cgi-faq.html</a>
  100. </pre>
  101. <p>If you still don't get it, try one of the
  102. ``get-rich-quick-by-writing-CGI-scripts'' books at your local bookstores.</p>
  103.  
  104. <h2><a name="How_do_I_return_a_graphics_file_">How do I return a graphics file
  105. from a CGI script?</a></h2>
  106. <p>One of the big differences between UNIX and Win32 platforms is that on Win32
  107. there's a difference between text or ASCII files and binary file. To return a
  108. graphics file, you need to specify that the file is a binary file, and that the
  109. standard output stream should accept binary data. Try something like this:</p>
  110. <pre>
  111.     $MY_FILE_NAME = 'Penelope.jpg';
  112.     $CHUNK_SIZE = 4096;
  113.     
  114.     open( MY_FILE, "<$MY_FILE_NAME" )
  115.         or die( "Can't open $MY_FILE_NAME: $!\n" );
  116.     
  117.     print "Content-type: image/jpeg\r\n";
  118.     print "\r\n";
  119.     
  120.     binmode( MY_FILE ); # These are crucial!
  121.     binmode( STDOUT );
  122.     
  123.     while ( $cb = read( MY_FILE, $data, $CHUNK_SIZE ) ) {
  124.         print $data;
  125.     }
  126.     
  127.     close( MY_FILE );
  128. </pre>
  129.  
  130. <h2><a name="My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't seem to run
  131. right under PerlIS.</a></h2>
  132. <p>Earlier versions of Perl for ISAPI did not output headers correctly, and this
  133. caused Perl for ISAPI scripts to misbehave. You could work around this in
  134. earlier versions of PerlIS by sending the headers at the beginning of your
  135. script like this:</p>
  136. <pre>
  137.     print <<"END";
  138.     HTTP/1.0 200 OK
  139.     Content-Type: text/html
  140.     
  141.     END
  142. </pre>
  143. <p>This is no longer necessary with the current version of Perl for ISAPI, as
  144. Perl for ISAPI sends the correct headers. The Registry value that governs this
  145. is <code>EnableCGIHeader</code>, a REG_DWORD value that is set to 1 by default.
  146. This value is stored under the Registry key
  147. HKEY_LOCAL_MACHINE\SOFTWARE\ActiveState\PerlIS. If you need to turn off the
  148. automatic generation of the header, set this value to 0.</p>
  149.  
  150. <h2><a name="How_does_my_script_know_if_it_s_">How does my script know if it's
  151. running under ActivePerl or Perl for ISAPI?</a></h2>
  152. <p>Perl for ISAPI sets an environment variable, <code>PERLXS</code>, for scripts
  153. that are executed under Perl for ISAPI.</p>
  154. <pre>
  155.     my $running_under_perlis = $ENV{PERLXS} eq 'PerlIS';
  156. </pre>
  157.  
  158. <h2><a name="How_does_my_script_know_if_it_s_">How does my script know if it's
  159. running under ActivePerl or Perl for WebSite?</a></h2>
  160. <p>Perl for WebSite sets an environment variable, <code>PERLXS</code>, for
  161. scripts that are executed under Perl for WebSite.</p>
  162. <pre>
  163.     my $running_under_perlws = $ENV{PERLXS} eq 'PerlWS';
  164. </pre>
  165.  
  166. <h2><a name="What_CGI_modules_run_with_Perl_f">What CGI modules run with
  167. ActivePerl?</a></h2>
  168. <p>The <em>CGI.pm</em> module works with ActivePerl. It's included with
  169. ActivePerl, and you can use it in your program with the statement <code>use CGI</code>.
  170. Complete documentation for the CGI module is embedded in the module, and can be
  171. read with <code>perldoc CGI</code>.</p>
  172.  
  173.  
  174. <h2><a name="How_do_I_use_redirection_in_my_s">How do I use redirection in my
  175. script?</a></h2>
  176. <p>You can use redirection to point the client browser to another file or
  177. script, usually in a way that's invisible to the user. The CGI module provides
  178. the <code>redirect()</code> function for this purpose. The following code
  179. redirects the client browser to <a class="doc" href="http://www.perl.com">http://www.perl.com</a>
  180. and works with both CGI or PerlIS:</p>
  181. <pre>
  182.     use CGI qw(:standard);
  183.     print redirect('<a class="doc" href="http://www.perl.com">http://www.perl.com</a>');
  184. </pre>
  185.  
  186. <h2><a name="What_are_cookies_and_how_do_I_us">What are cookies and how do I use
  187. them?</a></h2>
  188. <p>Cookies are packets of data that a server can give to a client, such as a
  189. browser, to maintain information even after the browser has left this site. This
  190. can be used to to identify the client in future interactions or to store user
  191. preferences. Cookies were first defined by Netscape, and the definition of
  192. cookies can be found at:</p>
  193. <pre>
  194.     <a class="doc" href="http://home.netscape.com/newsref/std/cookie_spec.html">http://home.netscape.com/newsref/std/cookie_spec.html</a> 
  195. </pre>
  196. <p>The CGI module provides support for managing cookies. See <a class="doc" href="#What_CGI_modules_run_with_Perl_f">What
  197. CGI modules run with ActivePerl?</a> for information about the CGI module,
  198. including the location of online documentation.</p>
  199.  
  200. <h2><a name="How_do_I_get_the_e_mail_address_">How do I get the e-mail address
  201. of the user?</a></h2>
  202. <p>It's not, in general, possible to get the e-mail address of a user without
  203. asking them for it. If you need an e-mail address, provide the user with a form
  204. to fill in an address.</p>
  205. <p>The HTTP specification (see <a class="doc" href="#What_is_HTTP_and_how_do_I_get_m">What
  206. is HTTP, and how do I get more information on it?</a>) defines an HTTP request
  207. header field, <code>From:</code>, that can contain the e-mail address of the
  208. user making the connection. Your script can retrieve it using the environment
  209. variable HTTP_FROM. However, it's rarely provided and can't really be counted
  210. on.</p>
  211. <p>There are all kinds of sneaky ways to determine where requests come from --
  212. like checking the IP address that was used to connect -- but you can't figure
  213. out the e-mail address of the user this way.</p>
  214. <p>Generally, it's considered a violation of the user's privacy to get their
  215. e-mail address without their knowledge. Perhaps that's why it's so difficult.</p>
  216.  
  217. <h2><a name="I_need_a_CGI_script_to_do_a_cert">I need a CGI script to do a
  218. certain task. Has anyone done it before?</a></h2>
  219. <p>There are several CGI script archives on the web. You may not be able to find
  220. exactly the script you want, but you will probably find something that can be
  221. adapted to your needs.</p>
  222. <p>Two of the most famous script pages are:</p>
  223. <ul>
  224.   <li>
  225.     <p>Jenda's pages: <a href="http://www24.brinkster.com/jenda">
  226.     www24.brinkster.com/jenda</a></p>
  227.   </li>
  228.   <li>
  229.     <p>The Perl Monks: <a href="http://www.perlmonks.org">www.perlmonks.org</a></p>
  230.   </li>
  231.   <li>
  232.     <p>Matt's script replacements: <a href="http://nms-cgi.sourceforge.net">
  233.     nms-cgi.sourceforge.net</a></p>
  234.   </li>
  235.   <li>
  236.     <p>Dave Doyle's Script Archive: <a class="doc" href="http://www.davedoyle.com/help/scripts.html">www.davedoyle.com/help/scripts.html</a></p>
  237.   </li>
  238. </ul>
  239. <p>Note that most script archives have UNIX-targeted scripts, and you may have
  240. to make some adaptations to them to make them run (see <a class="doc" href="ActivePerl-Winfaq5.html#How_do_I_make_a_UNIX_based_scrip">How
  241. do I make a UNIX-based script work?</a>).</p>
  242. <p>This is one of the most common questions on the Perl-Win32-Users list.
  243. Occasionally it's phrased more like "I have a program to write. Here are the 
  244. specs. Please e-mail it to me by Friday." Hopefully the reader can understand
  245. why these messages are usually answered rudely, if they are answered at all.</p>
  246.  
  247. <h2><a name="How_do_I_test_my_CGI_programs_th">How do I test my CGI programs
  248. that take arguments without running them through the web server?</a></h2>
  249. <p>The hard way to test your CGI programs from the command line is to set up
  250. environment variables and a standard input stream just like a web server would
  251. (see the CGI 1.1 specification for more details on this -- see <a class="doc" href="#What_is_CGI_and_how_do_I_get_mo">What
  252. is CGI, and how do I get more information on it?</a>).</p>
  253. <p>The easy way is to use <code>CGI.pm</code>, the Perl module for CGI
  254. programming. It provides an easy mechanism to run CGI programs, with arguments,
  255. from the command line. See <a class="doc" href="#What_CGI_modules_run_with_Perl_f">What CGI
  256. modules run with ActivePerl?</a>.</p>
  257.  
  258. <h2><a name="The_Content_Type_header_field_I">The Content-Type: header field I
  259. output from my CGI script shows up in the web browser.</a></h2>
  260. <p>You are using an older version of Perl for ISAPI as your Perl interpreter on
  261. an IIS web server, but it's not outputting an HTTP status line. You should be
  262. able to fix this by installing the most recent version of Perl and PerlIS. See <a class="doc" href="#My_CGI_scripts_don_t_seem_to_run">My
  263. CGI scripts don't seem to run right under Perl for ISAPI.</a> for more details.</p>
  264.  
  265. <h2><a name="When_I_try_to_run_a_CGI_script_f">When I try to run a CGI script
  266. from my browser, it tries to download a file of type
  267. "application/x-perl" instead.</a></h2>
  268. <p>Your web server has been misconfigured. It doesn't know that it should
  269. execute your Perl program, so it's just returning it to the browser. See <a class="doc" href="ActivePerl-Winfaq6.html">the
  270. perlwin32faq6 manpage</a> for details on how to configure your particular web
  271. server to know that Perl scripts should be executed.</p>
  272.  
  273. <!-- <h2><a name="What_s_Perl_for_WSAPI_">What's Perl for WSAPI?</a></h2>
  274. <p>Perl for WSAPI, or PerlWS for short, offers the same functionality as Perl
  275. for ISAPI to users of O'Reilly's WebSite Professional web server.</p> -->
  276.  
  277. <h2><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</a></h2>
  278. <p>This FAQ was originally assembled and maintained by Evangelo Prodromou. It
  279. has been revised and updated by Brian Jepson of O'Reilly & Associates, David
  280. Grove, David Dmytryshyn and David Sparks of ActiveState.</p>
  281. <p>This FAQ is in the public domain. If you use it, however, please ensure that
  282. you give credit to the original authors.</p>
  283.  
  284. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  285.   <tr>
  286.     <td class="block" valign="MIDDLE" width="100%" bgcolor="#cccccc"><strong>
  287.       <p class="block"> ActivePerl FAQ - Web Programming (CGI and PerlIS)</p>
  288.       </strong></td>
  289.   </tr>
  290. </table>
  291.  
  292. </body>
  293.  
  294. </html>
  295.