使选定的perl cookie无效



我有下面的perl脚本
我需要它来使不在validCookies哈希中的所有cookie无效。请注意,这只是我代码的上半部分,其余部分用于打印@cookieArray()中的所有cookie,这对我很有用。
由于正在设置正确的cookie,我需要稍后在代码中手动设置。ATM的代码并没有使cookie失效,有人明白为什么吗?

use CGI qw(:standard);
use CGI::Cookie;    

@cookieArray = ();
#hash of cookie names that should not be set to null
%validCookies = ( cName=> 0, cAddress => 0, cCity => 0, cProvince => 0, cPostalCode => 0, cMail => 0, cDate => 0);
%cook = CGI::Cookie->fetch;
foreach $name ($cook){
 if(exists ($validCookies{$name})){
 } else {
   $temp = CGI::Cookie->new(-name=>$name, -value =>"");
   push(@cookieArray, $temp);
 }
}

要使cookie无效,必须使其过期。以下代码将使除受保护的cookie名称之外的所有cookie过期。

不需要使用CGI::Cookie低级接口。所有功能都已通过cookie方法公开。

use strict;
use warnings FATAL => 'all';
use CGI qw();
use Data::Dumper qw(Dumper);
my %protected_names = map { $_ => undef }
    qw(cName cAddress cCity cProvince cPostalCode cMail cDate);
my $cgi = CGI->new;
print $cgi->header(
    -type => 'text/plain',
    -cookie => [
        map {
            $cgi->cookie(
                -name => $_,
                -value => (exists($protected_names{$_})
                    ? $cgi->cookie($_)
                    : q()
                ),
            )
        } $cgi->cookie
    ],
);
print Dumper [$cgi->cookie];

最新更新