⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 4 页
字号:


print "</form></center><hr>";



print "<center>";



# print "<table width=100%><tr><td>";



print "[ <a href=\"$base_url$edit_announce?$this_month&$this_year\">



       Add an announcement</a> for this month. ";



print "| <a href=\"$base_url$edit_announce?$this_month&$this_year&delete\">



    Delete an announcement</a> from this month." unless ($any_announce eq "no");



print " ]";



print "</center>";



print "<center>";



print "<hr>[ <a href=\"http://www.rcbowen.com/perl/HyperCal.html\">



       About HyperCal</a>.";



foreach $item (@linkto)    {



($url, $page_title)=split(/~~/,$item);



print " | <a href=\"$url\">$page_title</a>";



            }



if ($multi_user eq "yes")    {



print " | <a href=\"$base_url$change_passwd\">Change your user password</a>"}



print " ]</center><br>\n";



print "HyperCal, Version $version, Copyright © 1996, Richard Bowen.  



All rights reserved.<br>\n";



&footer;



    }    #  End of sub main



sub appoints   {



$found=0;



&julean($_[0],$_[1],$_[2]);  #  Julean date of day



for $entry (@datebook)    {



@temporary=split(/~~~/,$entry);



if (@temporary[0]==$jule)  {$found++}};



if ($found != 0) {print "   ($found)"};    }



sub goto    {



($pair1, $pair2, $pair3)=split(/&/,$args);



($junk, $month)=split(/=/, $pair1);



($junk, $year)=split(/=/, $pair2);



($junk, $this_year)=split(/=/, $pair3);



#  Need some error checking ...



if ($month eq ""){$month=1};



if ($year eq""){$year=$this_year};



if ($month>12) {$month=12};



if ($month<1) {$month=1};



if ($year<1) {$year=1};



if ($year>9999) {$year=9999};



$args="$month&$year";



&main



    }



sub personal    {



$sub=~s/personal\///;



$user_id=$ENV{`REMOTE_USER'};



$user_variables=~s/USERNAME/$user_id/;



require "$user_variables";



}



</FONT></PRE>



<P>Let's take a look at how HyperCal works. HyperCal uses a library called <TT>httools.pm</TT>,



which takes care of things like printing headers and parsing forms. CGI.pm could



be used instead of httools.pm with a few simple modifications to HyperCal's code.



The httools.pm module supports the following functions in HyperCal: <BR>



<BR>







<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD>



		<TD ALIGN="LEFT" VALIGN="TOP"><B>Usage and Description</B></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><TT>header</TT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP">Prints MIME content type header.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><TT>title</TT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP">Prints title for HTML page: <TT>&amp;title(`Desired title')</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><TT>form_parse</TT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP">Parses form, places variables in <TT>$FORM{`variable_name'}</TT>.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><TT>footer</TT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP">Prints generic footer.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><TT>date</TT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP">Returns today's date in nicer format.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><TT>julean</TT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP">Returns Julian date with Jan 1, 1995, as day 1.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="96" ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">todayjulean</FONT></TD>



		<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">Returns today's Julian date, calls julean.</FONT></TD>



	</TR>



</TABLE>



</P>



<P>HyperCal stores the URI encoded path info in the QUERY_STRING to a variable named



<TT>$args</TT>. Path info (variables encoded in the URI) is next filtered into the



variable <TT>$sub</TT>. The variable <TT>$sub</TT> is then evaluated to determine



what output the program should generate.</P>



<P>The hypercal.cgi program will do different things based on what variables are



present (or not present) in the QUERY_STRING (data appended to the URI) when it is



run. The principle behind this concept is simple. The HTML output (calendar) generated



by running the program contains HTML links back to the program. In other words, the



HTML calendar contains links and forms whose URIs reference back to the hypercal.cgi



script again. However, the links also contain variables appended to the URI. When



a user clicks on the URI with the appended variables, the program is run again. This



time it parses the variables in the URI and can react differently based on those



self-supplied variables. This is precisely what is happening in lines 15-17. If no



variables are found in the URI, the <TT>else</TT> statement instructs the program



to execute the <TT>&amp;main</TT> subroutine that prints the HTML for the calendar.</P>



<P>If variables are supplied to HyperCal in its URI, one of two subroutines is called



first. The <TT><BR>



&amp;personal</TT> subroutine determines if a personal calendar is being used and



alters a few variables accordingly. I'll discuss using personal calendars in more



depth in a moment. The other subroutine that may be called is <TT>&amp;goto</TT>.



The <TT>&amp;goto</TT> subroutine directs HyperCal to display a specific month. The



month and year must be supplied in the URI, in the following format:</P>



<PRE><A HREF="javascript:if(confirm('http://uri/hypercal.cgi/goto?month=9&year=1999&this_year=1996  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://uri/hypercal.cgi/goto?month=9&year=1999&this_year=1996'" tppabs="http://uri/hypercal.cgi/goto?month=9&year=1999&this_year=1996"><FONT



COLOR="#0066FF">http://URI/hypercal.cgi/goto?month=9&amp;year=1999&amp;this_year=1996



</FONT></A><FONT COLOR="#0066FF"></FONT></PRE>



<P>The <TT>&amp;goto</TT> subroutine reads the three name/value pairs, trashes the



names, and stores the values to <TT>$month</TT>, <TT>$year</TT>, and <TT>$this_year</TT>.



The month and year are then stored in <TT>$args</TT>, and the main subroutine is



called to display the requested month. Note that if <TT>$args</TT> does not contain



a prespecified month, the current month is used.</P>



<P>Using multiple users requires a few extra steps. In the configuration file (discussed



next), the <TT>$multi_user</TT> variable must be set to <TT>yes</TT>. Note that in



the <TT>&amp;main</TT> subroutine in hypercal.cgi, if this variable is set to <TT>yes</TT>,



a button <TT>Go To Personal Calendar</TT> is displayed.</P>



<P>Personal calendars are protected by restricting access to the user directory in



which HyperCal keeps the user data using the <TT>.htgroup</TT> and <TT>.htpasswd</TT>



file. The usage of <TT>.htpasswd</TT> is discussed more thoroughly in Chapter 3.



The cal_admin.cgi program is used to add users and passwords to the <TT>.htpasswd</TT>



file, essentially adding users to the calendar. To set up HyperCal in multi-user



mode, variables in the config file need to be set.



<CENTER>



<H4><A NAME="Heading5"></A><FONT COLOR="#000077">Configuration of HyperCalThe Variables



File</FONT></H4>



</CENTER>



<P>All of HyperCal's configuration variables are located in a separate file named



&quot;variables.&quot; By carefully reading over the configuration file, you can



get an idea of what HyperCal does and how to configure it. Listing 11.2 shows the



&quot;variables&quot; file for HyperCal.



<CENTER>



<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Listing 11.2. Variables and configuration



file for HyperCal.</FONT></H3>



</CENTER>



<PRE><FONT COLOR="#0066FF">



$version=&quot;2.3&quot;;



#  Names of the various program files



#  Some sites only allow cgi's with a .cgi extension



#  so you might need to change your file names



$add_date=&quot;add_date.cgi&quot;;



$del_date=&quot;del_date.cgi&quot;;



$disp_day=&quot;disp_day.cgi&quot;;



$edit_announce=&quot;edit_announce.cgi&quot;;



$hypercal=&quot;hypercal.cgi&quot;;



$cal_admin=&quot;cal_admin.cgi&quot;;



$personal=&quot;personal.cgi&quot;;



$change_passwd=&quot;change.password&quot;;



# Other files



$datebook=&quot;datebook&quot;;



$hypercal_id=&quot;hypercal_id&quot;;



$announce=&quot;announce&quot;;



$users_dir=&quot;/www/cgi-bin/datebook/users/&quot;;



$user_variables=&quot;/www/cgi-bin/datebook/users/USERNAME/variables&quot;;



# variables



#  Are you running a secure site with personal accounts?



$multi_user=&quot;no&quot;;



$personal_on=&quot;no&quot;;



$old=370;



#  Number of days to keep past dates



# Your info goes here - dont make this me, please



$admin=&quot;Richard Bowen&quot;;



$admin_mail=&quot;rbowen\@databeam.com&quot;;



$admin_uid=&quot;rbowen&quot;;



# Person to contact with problems



$base_url=&quot;/scripts/hypercal/&quot;;



# URL of the directory in which these files live



$title=&quot;HyperCal&quot;;



# Title of the calendar.



$htpass=&quot;/www/passwd/.htpasswd&quot;;



$htgroup=&quot;/www/passwd/.htgroup&quot;;



#  Location of the .htpasswd and .htgroup files



#  This will be used only if you are running the security part



@linkto=(&quot;http://www.mk.net/~~MK.net Home Page&quot;, &quot;/perl/~~My Perl archive&quot;);



#  Places to provide links to on each page.



#  This array contains the locations of images for the various



#  months.  The format is:



#  &quot;url_for_icon~~url_for_background~~bgcolor~~link~~visited link&quot;



#  This array must contain 12 elements. Any field where you have



#  no preference, indicate by &quot;none&quot;



@month_images=(&quot;images/january.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/february.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/march.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/april.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/may.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/june.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/july.gif~~none~~none~~none~~none~~none&quot;,



    &quot;none~~none~~none~~none~~none~~none&quot;,



    &quot;none~~none~~none~~none~~none~~none&quot;,



    &quot;images/october.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/november.gif~~none~~none~~none~~none~~none&quot;,



    &quot;images/december.gif~~none~~none~~none~~none~~none&quot;);



1;



</FONT></PRE>



<P>Notice which elements of the program are located in the configuration file. The



first two blocks define the names of the supplementary programs and the names and



locations of various configuration files. These locations are different on different



software platforms and flavors of UNIX, so placing them in a configuration file instead



of hard coding them into your code significantly increases your ability to easily



get your CGI running on different servers. In general, anything that you might want



to change, or will change based on the software environment of your CGI, should be



defined in a configuration file.</P>



<P>Arrays are a great way to store variables with multiple elements, like the <TT>month_images</TT>



in the <BR>



preceding example. Notice that the last line of code uses a return value to verify



the successful loading of the file to the main program.



<CENTER>



<H4><A NAME="Heading8"></A><FONT COLOR="#000077">Displaying Appointments on a Specific



Day: disp_day.cgi</FONT></H4>



</CENTER>



<P>Each day in the calendar is a link to disp_day.cgi, along with the date URI encoded.



For example, a link to June 25, 1997, looks like this:</P>



<PRE><FONT COLOR="#0066FF">http://myserver.com/hypercal/disp_day.cgi?6&amp;25&amp;1997



</FONT></PRE>



<P>The disp_day.cgi script, shown in Listing 11.3, simply reads the day from the



URI, reads the datebook database, looks up the Julian date to reference the database



entry, and formats an HTML page based on the contents of the database (if any) on



that day.



<CENTER>



<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Listing 11.3. disp_day.cgi.</FONT></H3>



</CENTER>



<PRE><FONT COLOR="#0066FF">#!/usr/bin/perl



#  Display Day.  Reads in database and prints appointments for the



#  selected day.  Allows option of adding new appointment.



#



require `variables';



require `httools.pl';



&amp;header;



#  Determine if this is a personal calendar;



($sub=$ENV{`PATH_INFO'})=~s#^/##;



if ($sub=~/personal/){require $personal};



#   Read date from QUERY_STRING



$info=$ENV{`QUERY_STRING'};



($month,$day,$year)=split(/&amp;/,$info);



#  Print titles to html page



&amp;month_txt(&quot;$month&quot;);



&amp;title(&quot;Appointments for $month_txt $day, $year.&quot;);



print &quot;&lt;h2&gt;Appointments for $month_txt $day, $year.&lt;/h2&gt;&lt;hr&gt;&quot;;



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -