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

📄 ch16.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
   print $q->submit(-name=>`Action',-value=>`Divide');



   print "\n</TD><TD>\n";



   print "</TR></TABLE>\n";



   print $q->end_form;



}



</FONT></PRE>



<P>You will note that in this example, we use the hidden field <TT>Value</TT> to



retain the current value of the calculator. We do some very basic field validation



to make sure the user actually gave us a number. Obviously, this calculator will



accept only integer values. Remember that when the user leaves our CGI program and



returns, all state is now lost. Figure 16.1 shows the calculator as it appears in



the Web browser.</P>



<P>Let's look at another example now in Listing 16.2 that will make use of a file



to retain state about a certain client. We will use the <TT>REMOTE_ADDR</TT> CGI



environment variable to distinguish between clients. In this example, we will be



allowing users to enter our Web site and write whatever they like in a big text field



and then store the contents of that field in a file for them to later come back to



and modify. Additionally, we will keep track of how many times users submitted changes



to their text and display that value to them. Our example will simply use the /tmp/visitors



directory to store these files and not worry about cleaning up these files. <BR>



<BR>



<A HREF="18wpp01.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/18wpp01.jpg"><TT><B>Figure 16.1.</B></TT></A><TT> </TT>The calculator



example in the browser.



<H3 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Listing 16.2. A personal



notepad.</FONT></H3>



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







$directory = &quot;/tmp/visitors&quot;;



use CGI::Form;



$q = new CGI::Form;



print $q-&gt;header();



print $q-&gt;start_html(-title=&gt;`A Personal Notepad');



print &quot;&lt;H1&gt;A Personal Notepad&lt;/H1&gt;\n&quot;;







$client=$q-&gt;cgi-&gt;var(`REMOTE_ADDR');



if ($client eq &quot;&quot;) {



   print &quot;&lt;P&gt;Sorry, I don't know who you are. I can't continue\n&quot;;



   exit;



} else {



   if ($q-&gt;cgi-&gt;var(`REQUEST_METHOD') eq `GET') {



      if (-e &quot;$directory/$client&quot;) {



         ($text,$visits)=&amp;getUserData(&quot;$directory/$client&quot;);



      } else {



         $text=&quot;&quot;;



         $visits=0;



      }



      &amp;printForm($q,$text,$visits);



   } else {



      if ($q-&gt;param(`Action') eq &quot;Submit&quot;) {



         ($whocares,$visits)=&amp;getUserData(&quot;$directory/$client&quot;);



         $text=$q-&gt;param(`Text');



         $visits++;



         &amp;setUserData(&quot;$directory/$client&quot;,$text,$visits);



      }



      &amp;printForm($q,$text,$visits);



   }



}



print $q-&gt;end_html();







sub printForm {



   my($q)=@_;



   $q-&gt;param(`Text',$text);



   print &quot;&lt;P&gt;You have modified this notepad $visits times.&lt;P&gt;\n&quot;;



   print $q-&gt;start_multipart_form();



   print $q-&gt;textarea(-name=&gt;`Text',-default=&gt;$text,



                      -rows=&gt;20,-columns=&gt;50);



   print &quot;&lt;BR&gt;&quot;;



   print $q-&gt;submit(-name=&gt;`Action',-value=&gt;`Submit');



   print $q-&gt;end_form;



}







sub getUserData {



   my($file)=@_;



   if (open(IN,&quot;&lt; $file&quot;)) {



      $visits=&lt;IN&gt;;



      $text=join(``,&lt;IN&gt;);



      close(IN);



   } else {



      $text=&quot;&quot;;



      $visits=0;



   }



   return($text,$visits);



}







sub setUserData {



   my($file,$text,$visits)=@_;



   if (open(OUT,&quot;&gt; $file&quot;)) {



      print OUT &quot;$visits\n&quot;;



      print OUT $text;



      close(OUT);



   } else {



      # This is an error condition that shouldn't happen.



      # Handle it properly just the same.



      print &quot;&lt;P&gt;Error! I cannot save your text. Sorry!&lt;BR&gt;\n&quot;;



   }



}



</FONT></PRE>



<P>This solution is probably more efficient if you need to save larger amounts of



data. However, this solution can also drain your server's resources very quickly.



Be sure that you have enough machine resources to handle the expected number of clients



for which you'll need to maintain state. Figure 16.2 shows the personal notepad as



it appears in the browser.</P>



<P>Hopefully, these examples will be enough to get you thinking about how you can



implement your own persistent CGI sessions across multiple transactions. <BR>



<BR>



<A HREF="18wpp02.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/18wpp02.jpg"><TT><B>Figure 16.2.</B></TT></A><TT> </TT>The personal



notepad. <BR>



<BR>



<B><TT>Execution and Shell-Like Interfaces</TT></B> Using this idea of preserving



session information, we can take the example further and provide a simple remote



login type of shell for execution of things on the server. What we'll do in Listing



16.3 is provide a login form, and each subsequent calls to the CGI program will be



a command line from which the user can remotely execute programs. This can also be



accomplished within a protected directory allowing the login action to be done within



the browser. You might also consider limiting the types of commands one could execute



using this CGI session.



<H3 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Listing 16.3. A Web-based



command shell.</FONT></H3>



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







use CGI::Form;



$q = new CGI::Form;



print $q-&gt;header();



print $q-&gt;start_html(-title=&gt;`A Web-based Command Shell');



print &quot;&lt;H1&gt;A Web-based Command Shell&lt;/H1&gt;\n&quot;;







if ($q-&gt;cgi-&gt;var(`REQUEST_METHOD') eq `GET') {



   &amp;loginForm($q);



} else {



   if ($q-&gt;param(`Action') eq &quot;Login&quot;) {



      $uid=$q-&gt;param(`uid');



      $pw=$q-&gt;param(`pw');



      if (&amp;validateLogin($uid,$pw)) {



         $history=&quot;&quot;;



         &amp;shellForm($q,$history);



      } else {



         &amp;unauthorized($q);



      }



   } elsif ($q-&gt;param(`Action') eq &quot;Doit&quot;) {



      $history=$q-&gt;param(`cmdHistory');



      $command=$q-&gt;param(`command');



      if ($command ne &quot;&quot;) {



         $history.=&amp;doCommand($q,$command);



         }



      $q-&gt;param(`cmdHistory',$history);



      &amp;shellForm($q,$history);



   } else {



      &amp;unauthorized($q);



   }



}



print $q-&gt;end_html();







sub validateLogin {



   my($userid,$pw)=@_;



   my($retval)=0;



   if (open(PASSWD,&quot;/etc/passwd&quot;)) {



      while (&lt;PASSWD&gt;) {



         chop;



         my($login,$passwd,$uid,$gid,$rest)=split(/:/);



         if ($login eq $userid &amp;&amp; crypt($pw,$passwd) eq $passwd) {



            $retval=1;



            last;



         }



      }



      close(PASSWD);



   }



   return $retval;



}







sub unauthorized {



   my($q)=@_;



⌨️ 快捷键说明

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