顺手写下,防止忘掉。
草稿,估计定稿不会是这个 :D
原因: 网站下同时存在多个相同的程序的副本,但是并不想 cookie 相互影响;
代码如下
###---- cookie By EasunLee 2012-03-11--------### sub get_cookie_path { if ($esB::INFO->{'COOKIE_USED'} eq 1) { $esB::INFO->{'cookiepath'} ='';} else { my $boardurltemp = $esB::INFO->{'BBS_URL'}; $boardurltemp =~ s/http\:\/\/(\S+?)\/(.*)/\/$2/; $esB::INFO->{'cookiepath'} = $boardurltemp; $esB::INFO->{'cookiepath'} =~ s/\/$//; } $esB::INFO->{'cookiepath'} ; } # 设定cookie sub set_cookie_str { my ($name,$value,$expires) =@_; &get_cookie_path() unless ( defined ( $esB::INFO->{'cookiepath'} ) ) ; my $prefix = $esB::INFO->{'cookie_prefix'} ; $esB::CGI->cookie(-name => $prefix.$name, -value => $value, -path => $esB::INFO->{'cookiepath'}.'/', -expires => $expires); } # 读取cookie sub get_cookie_str { my $name =shift; &get_cookie_path() unless ( defined ( $esB::INFO->{'cookiepath'} ) ) ; my $prefix = $esB::INFO->{'cookie_prefix'} ; $esB::CGI->cookie (-name => $prefix.$name) ;#, -path => $esB::INFO->{'cookiepath'}.'/'); } #### End cookie subs
Update , 最后定稿:
sub esbcookie
{
my ($self,$name,$value,$expires) =@_;
$self->get_cookie_path() unless ( defined ( $esB::INFO->{'cookiepath'} ) ) ;
my $prefix = $esB::INFO->{'cookie_prefix'} ;
my %infos=() ;
$infos{'-path'} = $esB::INFO->{'cookiepath'}.'/' ;
$infos{'-name'} = $esB::INFO->{'cookie_prefix'}.$name;
$infos{'-value'} = $value if defined($value);
$infos{'-expires'} = $expires if defined($expires);
# $esB::CGI->cookie(-name => $prefix.$name, -value => $value, -path => $esB::INFO->{'cookiepath'}.'/', -expires => $expires);
$esB::CGI->cookie(%infos);
}
其实就是给每个 cookie 的 name 前加了一段前缀,让它和其他程序分开。
最终定稿:
正需要,学习了。