【发布时间】:2013-11-15 19:50:24
【问题描述】:
我知道如何在 cookie 中传递一个值(变量),但是如何在一个 cookie 中发送多个值。这是我做cookie的方法
发送:
my $name = $query->param('name');
print "Set-Cookie:name=$name\n";
阅读:
$name = cookie('name');
现在我有了其他变量,例如身高、体重等。我该怎么做?
提前致谢
【问题讨论】:
我知道如何在 cookie 中传递一个值(变量),但是如何在一个 cookie 中发送多个值。这是我做cookie的方法
发送:
my $name = $query->param('name');
print "Set-Cookie:name=$name\n";
阅读:
$name = cookie('name');
现在我有了其他变量,例如身高、体重等。我该怎么做?
提前致谢
【问题讨论】:
可能有更好的方法,但这是一种便宜且容易想到的方法:
use MIME::Base64; # for encode_base64 / decode_base64
use YAML::XS; # for Dump and Load
# Setting the cookie:
# -- Put the keys you want to store in a hash
# -- encode as follows:
my $cookie_value = encode_base64( Dump( \%hash ) );
print "Set-Cookie:monster=$cookie_value\n";
# To decode the cookie:
# -- Get the cookie value into a string
# -- Decode into a hashref as follows:
my $cookie_hash = Load( decode_base64( $cookie_value ) );
这会让你在 cookie 中放多少你想放多少就放多少,不管 cookie 的最大长度是多少。
您可以在 cpan.org 上找到这些模块:
【讨论】: