我如何获得会议

时间:2020-07-23 09:49:41

标签: perl cookies

我试图添加一个设置cookie并将其读取到现有Perl脚本的尝试。我在https://alvinalexander.com/perl/edu/articles/pl010012/找到了示例代码,并且当它们自己运行时,这两个示例脚本确实可以工作。因此,我尝试设置功能以在现有脚本中使用这些功能。他们在这里

sub set_cookie
{
#------------------------------#
#  1. Create a new CGI object  #
#------------------------------#

my $session = shift;

my $query;
my $cookie;
my $cookie1;
my $cookie2;
my $theCookie;
my $val;
my $id;


$query = new CGI;

logit("Entering set_cookie() called with $session\n");

#------------------------------------------------------------------------------#
#  2. Create the desired cookie using the cookie() method.
#     Do this before calling the header() method, because the cookie must be
#     incorporated into the HTTP header.
#
#------------------------------------------------------------------------------#

$val = "Session_ID=$session";
logit("Cookie val set to $val\n");

$cookie = $query->cookie(-name=>'MY_COOKIE',
                         -value=>$val,
                         -expires=>'+10m',
                         -path=>'/');

$cookie1 = CGI::Cookie->new(-name=>'ID',
                                -expires=>'+10m',
                                -value=>123456);


$cookie2 = CGI::Cookie->new(-name=>'preferences',
                           -value=>{ font => 'Helvetica',
                                     size => 12 }
                           );

print header(-cookie=>[$cookie1,$cookie2]);

my %cookies = CGI::Cookie->fetch;
$id = $cookies{'ID'}->value;

logit("Cookie id set to $id\n");
logit("Cookie val set to $val\n");

#--------------------------------------------------------------#
#  3. Create the HTTP header and print the doctype statement.  #
#--------------------------------------------------------------#

print $query->header(-cookie => $cookie);
$theCookie = $query->cookie('MY_COOKIE');
print $query->end_html;sub read_cookie
{
#------------------------------#
#  1. Create a new CGI object  #
#------------------------------#

my $rval = 0;
my $theCookie ='';
my $name;
my $string;

my $query = new CGI;

logit("Entering read_cookie()\n");

#--------------------------------------------------------------#
#  2. Create the HTTP header and print the doctype statement.  #
#--------------------------------------------------------------#

print $query->header;


#----------------------------------------------------#
#  3. Start the HTML doc, and give the page a title  #
#----------------------------------------------------#

print $query->start_html('My cookie-get.cgi program');


#----------------------------------------------------------------------#
#  4. Retrieve the cookie. Do this by using the cookie method without  #
#     the -value parameter.                                            #
#----------------------------------------------------------------------#


# print $query->h3('The cookie is ...');
$theCookie = $query->cookie('MY_COOKIE');

if(!$theCookie) {
        $theCookie = '';
}

# logit("theCookie = $theCookie\n");
if (index($theCookie, 'ID') != -1) {
        $rval = 1;
}


logit("read_cookie() returning rval = $rval. Having read a value of $theCookie\n");
print $query->end_html;
return($rval);
}
 

l``但这不起作用。我查看了示例,似乎set_cookie脚本在声明HTML标头之前发送了set-cookie,这对我来说似乎不正确。我这样称呼这些

   if (!read_cookie()) {        #get the session id for later use
            $id = $session{_session_id};
    #       print "\n\nSession ID $id\n";
            set_cookie($id);
            return($id);
    }
    I would GREATLY appreciate some guidance here as my frustration level is very high
             $session_id = start_session($cgi);
    }
     and start_sessions calls set_cookie 

我使用Apache :: Session :: Store :: Postgres;参加会议

1 个答案:

答案 0 :(得分:0)

我拿走了您的代码并将其清理干净,这样我可以更好地阅读它,并在其中添加注释以显示我的更改。为了使第一个子例程能够在非常简单的单元(例如测试(set_cookie))中正常工作,需要进行一些更改。 set_cookies中的修复程序后,read_cookies()似乎可以工作。不过,您可能仍然需要对其进行调整。

希望这对您至少有帮助。

#######################################################################
sub set_cookie {
    #------------------------------#
    #  1. Create a new CGI object  #
    #------------------------------#

    my $session = shift;

    my $query;
    my $cookie;
    my $cookie1;
    my $cookie2;
    my $theCookie;
    my $val;
    my $id;


    $query = new CGI;

    logit("Entering set_cookie() called with $session\n");

    #------------------------------------------------------------------------------#
    #  2. Create the desired cookie using the cookie() method.
    #     Do this before calling the header() method, because the cookie must be
    #     incorporated into the HTTP header.
    #
    #------------------------------------------------------------------------------#

    $val = "Session_ID=$session";
    logit("Cookie val set to $val\n");

    $cookie = $query->cookie(-name=>'MY_COOKIE',
                             -value=>$val,
                             -expires=>'+10m',
                             -path=>'/');

    # TOOK OUT THESE 2 COOKIES FOR TESTING AND USED $query->cookie()                        
    #$cookie1 = CGI::Cookie->new(-name=>'ID',
    #                                -expires=>'+10m',
    #                                -value=>123456);

    #$cookie2 = CGI::Cookie->new(-name=>'preferences',
    #                           -value=>{ font => 'Helvetica',
    #                                     size => 12 }
    #                           );

    $cookie1 = $query->cookie(-name=>'ID',
                                    -expires=>'+10m',
                                    -value=>123456);

    $cookie2 = $query->cookie(-name=>'preferences',
                               -value=>{ font => 'Helvetica',
                                         size => 12 }
                               );
    # MISSING $query->
    #print header(-cookie=>[$cookie1,$cookie2]);

    # ADDED $query-> AND ADDED $cookie
    print $query->header(-cookie=>[$cookie,$cookie1,$cookie2]);

    my %cookies = CGI::Cookie->fetch;
    $id = $cookies{'ID'}->value;

    logit("Cookie id set to $id\n");
    logit("Cookie val set to $val\n");

    #--------------------------------------------------------------#
    #  3. Create the HTTP header and print the doctype statement.  #
    #--------------------------------------------------------------#

    # DON'T NEED THIS - $cookie ADDED TO PREVIOUS print header()
    #print $query->header(-cookie => $cookie);

    # Redundant? 
    $theCookie = $query->cookie('MY_COOKIE');

    print $theCookie;
    print $query->end_html;

# ADDED CLOSING BRACKET FOR sub set_cookie
}

#######################################################################
# This seems ok. Tested separately after fixing sub set_cookies
sub read_cookie {
    #------------------------------#
    #  1. Create a new CGI object  #
    #------------------------------#

    my $rval = 0;
    my $theCookie ='';
    my $name;
    my $string;

    my $query = new CGI;

    logit("Entering read_cookie()\n");

    #--------------------------------------------------------------#
    #  2. Create the HTTP header and print the doctype statement.  #
    #--------------------------------------------------------------#

    print $query->header;


    #----------------------------------------------------#
    #  3. Start the HTML doc, and give the page a title  #
    #----------------------------------------------------#

    print $query->start_html('My cookie-get.cgi program');


    #----------------------------------------------------------------------#
    #  4. Retrieve the cookie. Do this by using the cookie method without  #
    #     the -value parameter.                                            #
    #----------------------------------------------------------------------#


    # print $query->h3('The cookie is ...');
    $theCookie = $query->cookie('MY_COOKIE');

    if(!$theCookie) {
            $theCookie = '';
    }

    logit("theCookie = $theCookie\n");

    if (index($theCookie, 'ID') != -1) {
            $rval = 1;
    }


    logit("read_cookie() returning rval = $rval. Having read a value of $theCookie\n");

    print $query->end_html;
    return($rval);
}
#######################################################################