需要使用Perl设置和读取cookie
我尝试添加一个cookie设置,并将其读取到现有的Perl脚本中。我在上找到了示例代码,事实上,这两个示例脚本都可以自己运行。因此,我尝试设置函数,以便在现有脚本中使用这些函数。给你需要使用Perl设置和读取cookie,perl,cookies,Perl,Cookies,我尝试添加一个cookie设置,并将其读取到现有的Perl脚本中。我在上找到了示例代码,事实上,这两个示例脚本都可以自己运行。因此,我尝试设置函数,以便在现有脚本中使用这些函数。给你 sub set_cookie { #------------------------------# # 1. Create a new CGI object # #------------------------------# my $session = shift; my $query; my $cook
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);
}
但这是行不通的。我看过这些示例,似乎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
$id=$session{u session\u id}
在哪里定义了%会话
哈希?我的$id;我的%session;tie%session,'Apache::session::Postgres',$id,{Handle=>$dbh}$session\u id=start\u session($cgi)
哪里定义了start\u session()
?$id=$session{{u session\u id}
在哪里定义了%会话
哈希?我的$id;我的%session;tie%session,'Apache::session::Postgres',$id,{Handle=>$dbh}$session\u id=start\u session($cgi)
在哪里定义了start\u session()?