home *** CD-ROM | disk | FTP | other *** search
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
-
- <head>
- <title>ActivePerl FAQ - Web Programming</title>
- <link rel="STYLESHEET" href="../../Active.css" type="text/css" media="screen">
- </head>
-
- <body>
-
- <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAE2BB">
- <tr>
- <td width="57"><a target=_blank href="http://www.ActiveState.com/ActivePerl/">
- <img src="../../images/activeperl_logo.gif" width="57" height="48" border="0" alt="ActivePerl"></a></td>
- <td><div align="center" class="heading">ActivePerl User Guide</div></td>
- <td width="112"><a target=_blank href="http://www.ActiveState.com">
- <img src="../../images/AS_logo.gif" width="112" height="48" border="0" alt="ActiveState" /></a></td>
- </tr>
- <tr>
- <td class="lineColour" colspan="3"></td>
- </tr>
- </table>
-
- <h1>Web Programming (CGI and PerlIS)</h1>
-
- <ul>
- <li><a class="doc" href="#NAME">NAME</a></li>
- <li><a class="doc" href="#DESCRIPTION">DESCRIPTION</a>
- <ul>
- <li><a class="doc" href="#What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I
- get more information on it?</a></li>
- <li><a class="doc" href="#What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I
- get more information on it?</a></li>
- <li><a class="doc" href="#How_do_I_return_a_graphics_file_">How do I return a graphics
- file from a CGI script?</a></li>
- <li><a class="doc" href="#My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't seem
- to run right under PerlIS.</a></li>
- <li><a class="doc" href="#How_does_my_script_know_if_it_s_">How does my script know if
- it's running under ActivePerl or</a></li>
- <li><a class="doc" href="#What_CGI_modules_run_with_Perl_f">What CGI modules run with
- ActivePerl?</a></li>
- <li><a class="doc" href="#How_do_I_use_redirection_in_my_s">How do I use redirection
- in my script?</a></li>
- <li><a class="doc" href="#What_are_cookies_and_how_do_I_us">What are cookies and how
- do I use them?</a></li>
- <li><a class="doc" href="#How_do_I_get_the_e_mail_address_">How do I get the e-mail
- address of the user?</a></li>
- <li><a class="doc" href="#I_need_a_CGI_script_to_do_a_cert">I need a CGI script to do
- a certain task. Has anyone done it before?</a></li>
- <li><a class="doc" href="#How_do_I_test_my_CGI_programs_th">How do I test my CGI
- programs that take arguments without running</a></li>
- <li><a class="doc" href="#The_Content_Type_header_field_I">The Content-Type: header
- field I output from my CGI script shows up</a></li>
- <li><a class="doc" href="#When_I_try_to_run_a_CGI_script_f">When I try to run a CGI
- script from my browser, it tries to</a></li>
- <!-- <li><a class="doc" href="#What_s_Perl_for_WSAPI_">What's Perl for WSAPI?</a></li> -->
- </ul>
- </li>
- <li><a class="doc" href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</a></li>
- </ul>
-
- <h2><a name="NAME">NAME</a></h2>
- <p>ActivePerl-faq7 - Web Programming (CGI and PerlIS)</p>
-
- <h2><a name="DESCRIPTION">DESCRIPTION</a></h2>
- <p>Web development with ActivePerl</p>
-
- <h2><a name="What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I get
- more information on it?</a></h2>
- <p>HTTP (HyperText Transfer Protocol) is the protocol that web browsers use to
- talk to web servers. The official specification for the HTTP standard is
- available on the W3 Consortium web server at:</p>
- <pre>
- <a class="doc" href="http://www.w3.org/pub/WWW/Protocols/">http://www.w3.org/pub/WWW/Protocols/</a>
- </pre>
- <p>Some more readable introductions can be found on the Yahoo HTTP page at:</p>
- <pre>
- <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>
- </pre>
-
- <h2><a name="What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I get more
- information on it?</a></h2>
- <p>CGI (Common Gateway Interface) is a protocol used by web servers to run
- server programs. Scripts that support the CGI protocol are sometimes called
- ``CGI scripts''; this leads to the unfortunate misperception that CGI is a
- language of its own.</p>
- <p>The classic information on CGI is available on the NCSA server at:</p>
- <pre>
- <a class="doc" href="http://hoohoo.ncsa.uiuc.edu/cgi/">http://hoohoo.ncsa.uiuc.edu/cgi/</a>
- </pre>
- <p>If you haven't read this, read it now. If it doesn't click for you, you can
- check the Yahoo CGI page at:</p>
- <pre>
- <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>
- </pre>
- <p>(All one URL)</p>
- <p>Try also the following URL for CGI programming in Perl:</p>
- <pre>
- <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>
- </pre>
- <p>If you still don't get it, try one of the
- ``get-rich-quick-by-writing-CGI-scripts'' books at your local bookstores.</p>
-
- <h2><a name="How_do_I_return_a_graphics_file_">How do I return a graphics file
- from a CGI script?</a></h2>
- <p>One of the big differences between UNIX and Win32 platforms is that on Win32
- there's a difference between text or ASCII files and binary file. To return a
- graphics file, you need to specify that the file is a binary file, and that the
- standard output stream should accept binary data. Try something like this:</p>
- <pre>
- $MY_FILE_NAME = 'Penelope.jpg';
- $CHUNK_SIZE = 4096;
-
- open( MY_FILE, "<$MY_FILE_NAME" )
- or die( "Can't open $MY_FILE_NAME: $!\n" );
-
- print "Content-type: image/jpeg\r\n";
- print "\r\n";
-
- binmode( MY_FILE ); # These are crucial!
- binmode( STDOUT );
-
- while ( $cb = read( MY_FILE, $data, $CHUNK_SIZE ) ) {
- print $data;
- }
-
- close( MY_FILE );
- </pre>
-
- <h2><a name="My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't seem to run
- right under PerlIS.</a></h2>
- <p>Earlier versions of Perl for ISAPI did not output headers correctly, and this
- caused Perl for ISAPI scripts to misbehave. You could work around this in
- earlier versions of PerlIS by sending the headers at the beginning of your
- script like this:</p>
- <pre>
- print <<"END";
- HTTP/1.0 200 OK
- Content-Type: text/html
-
- END
- </pre>
- <p>This is no longer necessary with the current version of Perl for ISAPI, as
- Perl for ISAPI sends the correct headers. The Registry value that governs this
- is <code>EnableCGIHeader</code>, a REG_DWORD value that is set to 1 by default.
- This value is stored under the Registry key
- HKEY_LOCAL_MACHINE\SOFTWARE\ActiveState\PerlIS. If you need to turn off the
- automatic generation of the header, set this value to 0.</p>
-
- <h2><a name="How_does_my_script_know_if_it_s_">How does my script know if it's
- running under ActivePerl or Perl for ISAPI?</a></h2>
- <p>Perl for ISAPI sets an environment variable, <code>PERLXS</code>, for scripts
- that are executed under Perl for ISAPI.</p>
- <pre>
- my $running_under_perlis = $ENV{PERLXS} eq 'PerlIS';
- </pre>
-
- <h2><a name="How_does_my_script_know_if_it_s_">How does my script know if it's
- running under ActivePerl or Perl for WebSite?</a></h2>
- <p>Perl for WebSite sets an environment variable, <code>PERLXS</code>, for
- scripts that are executed under Perl for WebSite.</p>
- <pre>
- my $running_under_perlws = $ENV{PERLXS} eq 'PerlWS';
- </pre>
-
- <h2><a name="What_CGI_modules_run_with_Perl_f">What CGI modules run with
- ActivePerl?</a></h2>
- <p>The <em>CGI.pm</em> module works with ActivePerl. It's included with
- ActivePerl, and you can use it in your program with the statement <code>use CGI</code>.
- Complete documentation for the CGI module is embedded in the module, and can be
- read with <code>perldoc CGI</code>.</p>
-
-
- <h2><a name="How_do_I_use_redirection_in_my_s">How do I use redirection in my
- script?</a></h2>
- <p>You can use redirection to point the client browser to another file or
- script, usually in a way that's invisible to the user. The CGI module provides
- the <code>redirect()</code> function for this purpose. The following code
- redirects the client browser to <a class="doc" href="http://www.perl.com">http://www.perl.com</a>
- and works with both CGI or PerlIS:</p>
- <pre>
- use CGI qw(:standard);
- print redirect('<a class="doc" href="http://www.perl.com">http://www.perl.com</a>');
- </pre>
-
- <h2><a name="What_are_cookies_and_how_do_I_us">What are cookies and how do I use
- them?</a></h2>
- <p>Cookies are packets of data that a server can give to a client, such as a
- browser, to maintain information even after the browser has left this site. This
- can be used to to identify the client in future interactions or to store user
- preferences. Cookies were first defined by Netscape, and the definition of
- cookies can be found at:</p>
- <pre>
- <a class="doc" href="http://home.netscape.com/newsref/std/cookie_spec.html">http://home.netscape.com/newsref/std/cookie_spec.html</a>
- </pre>
- <p>The CGI module provides support for managing cookies. See <a class="doc" href="#What_CGI_modules_run_with_Perl_f">What
- CGI modules run with ActivePerl?</a> for information about the CGI module,
- including the location of online documentation.</p>
-
- <h2><a name="How_do_I_get_the_e_mail_address_">How do I get the e-mail address
- of the user?</a></h2>
- <p>It's not, in general, possible to get the e-mail address of a user without
- asking them for it. If you need an e-mail address, provide the user with a form
- to fill in an address.</p>
- <p>The HTTP specification (see <a class="doc" href="#What_is_HTTP_and_how_do_I_get_m">What
- is HTTP, and how do I get more information on it?</a>) defines an HTTP request
- header field, <code>From:</code>, that can contain the e-mail address of the
- user making the connection. Your script can retrieve it using the environment
- variable HTTP_FROM. However, it's rarely provided and can't really be counted
- on.</p>
- <p>There are all kinds of sneaky ways to determine where requests come from --
- like checking the IP address that was used to connect -- but you can't figure
- out the e-mail address of the user this way.</p>
- <p>Generally, it's considered a violation of the user's privacy to get their
- e-mail address without their knowledge. Perhaps that's why it's so difficult.</p>
-
- <h2><a name="I_need_a_CGI_script_to_do_a_cert">I need a CGI script to do a
- certain task. Has anyone done it before?</a></h2>
- <p>There are several CGI script archives on the web. You may not be able to find
- exactly the script you want, but you will probably find something that can be
- adapted to your needs.</p>
- <p>Two of the most famous script pages are:</p>
- <ul>
- <li>
- <p>Jenda's pages: <a href="http://www24.brinkster.com/jenda">
- www24.brinkster.com/jenda</a></p>
- </li>
- <li>
- <p>The Perl Monks: <a href="http://www.perlmonks.org">www.perlmonks.org</a></p>
- </li>
- <li>
- <p>Matt's script replacements: <a href="http://nms-cgi.sourceforge.net">
- nms-cgi.sourceforge.net</a></p>
- </li>
- <li>
- <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>
- </li>
- </ul>
- <p>Note that most script archives have UNIX-targeted scripts, and you may have
- 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
- do I make a UNIX-based script work?</a>).</p>
- <p>This is one of the most common questions on the Perl-Win32-Users list.
- Occasionally it's phrased more like "I have a program to write. Here are the
- specs. Please e-mail it to me by Friday." Hopefully the reader can understand
- why these messages are usually answered rudely, if they are answered at all.</p>
-
- <h2><a name="How_do_I_test_my_CGI_programs_th">How do I test my CGI programs
- that take arguments without running them through the web server?</a></h2>
- <p>The hard way to test your CGI programs from the command line is to set up
- environment variables and a standard input stream just like a web server would
- (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
- is CGI, and how do I get more information on it?</a>).</p>
- <p>The easy way is to use <code>CGI.pm</code>, the Perl module for CGI
- programming. It provides an easy mechanism to run CGI programs, with arguments,
- from the command line. See <a class="doc" href="#What_CGI_modules_run_with_Perl_f">What CGI
- modules run with ActivePerl?</a>.</p>
-
- <h2><a name="The_Content_Type_header_field_I">The Content-Type: header field I
- output from my CGI script shows up in the web browser.</a></h2>
- <p>You are using an older version of Perl for ISAPI as your Perl interpreter on
- an IIS web server, but it's not outputting an HTTP status line. You should be
- 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
- CGI scripts don't seem to run right under Perl for ISAPI.</a> for more details.</p>
-
- <h2><a name="When_I_try_to_run_a_CGI_script_f">When I try to run a CGI script
- from my browser, it tries to download a file of type
- "application/x-perl" instead.</a></h2>
- <p>Your web server has been misconfigured. It doesn't know that it should
- execute your Perl program, so it's just returning it to the browser. See <a class="doc" href="ActivePerl-Winfaq6.html">the
- perlwin32faq6 manpage</a> for details on how to configure your particular web
- server to know that Perl scripts should be executed.</p>
-
- <!-- <h2><a name="What_s_Perl_for_WSAPI_">What's Perl for WSAPI?</a></h2>
- <p>Perl for WSAPI, or PerlWS for short, offers the same functionality as Perl
- for ISAPI to users of O'Reilly's WebSite Professional web server.</p> -->
-
- <h2><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</a></h2>
- <p>This FAQ was originally assembled and maintained by Evangelo Prodromou. It
- has been revised and updated by Brian Jepson of O'Reilly & Associates, David
- Grove, David Dmytryshyn and David Sparks of ActiveState.</p>
- <p>This FAQ is in the public domain. If you use it, however, please ensure that
- you give credit to the original authors.</p>
-
- <table border="0" cellpadding="0" cellspacing="0" width="100%">
- <tr>
- <td class="block" valign="MIDDLE" width="100%" bgcolor="#cccccc"><strong>
- <p class="block"> ActivePerl FAQ - Web Programming (CGI and PerlIS)</p>
- </strong></td>
- </tr>
- </table>
-
- </body>
-
- </html>
-