【问题标题】:CGI script to set a cookie and reload用于设置 cookie 并重新加载的 CGI 脚本
【发布时间】:2012-02-29 20:16:05
【问题描述】:

我有一个 CGI 脚本,它在 URL 中获取一些参数,并呈现一个表单。 当用户提交表单时,表单中的信息会保存在 cookie 中。 现在,在提交时,我希望调用相同的 URL,包括最初调用它的参数。

示例:也许更清楚: 有人这样叫我:

www.mysite.com/script.cgi?name=nec&mail=necnec

在这个页面上,用户可以选择颜色(红、黄、绿)。 当按下按钮提交时,我希望我的页面被称为:

www.mysite.com/script.cgi?name=nec&mail=necnec&color=green

我该怎么做?

谢谢!!!

【问题讨论】:

    标签: perl cgi


    【解决方案1】:

    您的名为 script.cgi 的脚本可能如下所示。

    script.cgi

    #!C:\Perl\bin\perl.exe
    use strict;
    use warnings;  
    use CGI;  
    my $query = new CGI;  
    my $p_name=$query->param('name')|| "NO Name";  
    my $p_mail=$query->param('mail')|| "NO Email";  
    print $query->header( "text/html" );  
    print <<START_HERE;  
           <html>  
           <head>  
               <title>Your First CGI Script</title>  
           </head>  
           <body>  
               <h1>This is a script Web page</h1>  
               <p>  
                  <form name='testform' method='get' action='test.pl'>  
                  <input type="hidden" name="name" value=$p_name />  
                  <input type="hidden" name="mail" value=$p_mail /> 
                  <select name='color'>  
                          <option value="red">Red</option>  
                          <option value="green">Green</option>  
                          <option value="white">White</option>  
                          <option value="yellow">Yellow</option>  
                   </select>  
                   <input type='submit' value='submit' name="submit" />  
                   </form>  
                </p>  
              </body>  
              </html>  
              START_HERE 
        #must have a line after "START_HERE" or Perl won't recognize  
        #the token 
    

    使用参数方法 script.cgi 将采用参数 namema​​il 和 提交表单后,此页面将带您到 test.pl。这里 colornamema​​il 参数可用。因为 name 和 mail 是使用隐藏字段发送的。

    test.pl

    #!C:\Perl\bin\perl.exe
    use strict;  
    use warnings;  
    use CGI;  
    my $query = new CGI;  
    my $cookie_color=$query->cookie('color');
    my $name=$query->param('name')|| "NO Name";  
    my $mail=$query->param('mail')|| "NO Email";  
    my $color=$query->param('color')|| $cookie_color || "NO color";
    my $cookie = $query->cookie(-name=>'color',
        -value=>$color_value,
        -expires=>'+4h',
        -path=>'/');
    print $query->header( "text/html" );  
    print $query->header(-cookie=>$cookie);
    print <<START_HERE;  
             <html>  
               <head>  
                  <title>Script to check the Parameters</title>  
               </head>  
               <body>  
                  <h1>This is a test Web page</h1>  
                  <p>Name: $name</p>  
                  <p>Mail: $mail</p>  
                  <p>Color: $color</p>  
               </body>  
            </html>  
           START_HERE   
    #must have a line after "START_HERE" or Perl won't recognize  
    #the token 
    

    您也可以在 testform 中使用 action='script.cgi'。并在 script.cgi 中显示 namema​​ilcolor 类似于 test.pl >.

    Cookie 可以在 test.pl 中设置。那是在提交表单之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      相关资源
      最近更新 更多