标签/Tag为[code]的文章

[Perl]一段cookie操作的封装代码

顺手写下,防止忘掉。
草稿,估计定稿不会是这个 :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