perl - 需要使用 Perl 设置和读取 cookie
问题描述
我试图添加设置 cookie,并将其读取到现有的 Perl 脚本中。我在https://alvinalexander.com/perl/edu/articles/pl010012/找到了示例代码,并且确实这 2 个示例脚本在自己运行时有效。因此,我尝试设置功能以在现有脚本中使用这些功能。他们来了
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
解决方案
推荐阅读
- c - 尝试 scanf 但无法访问它的值,除非输入另一个值
- sql - Sql server xpath 条件查询
- android - 有没有办法在 OnPause 事件或类似于 Android 中的 SavedInstanceState 的东西上保存科尔多瓦应用程序实例
- database - 使用查询将数据复制到 postgresql 中的 json 文件中时,将单个“\”的记录替换为“\\”
- lua - 传递给 Pandoc 的变量可以在 lua 过滤器中使用吗?
- c# - C# 类对象列表更改以匹配最后一个对象?
- html - 高度/宽度为 100% 的 DIV 中的 iFrame
- kubernetes - 如何在 GKE 上为本地 SSD 磁盘获得更好的性能?
- azure - 如何让 Kudu 从我的仓库中删除一些文件夹?
- python - 如何在没有 PSUtil 的情况下使用 Python 2.7 获取总磁盘空间、已使用和可用磁盘空间