0

我知道如何在 cookie 中传递一个值(变量),但是如何在一个 cookie 中发送多个值。这是我做饼干的方法

发送:

my $name = $query->param('name');
print "Set-Cookie:name=$name\n";

阅读:

$name = cookie('name');

现在我有其他变量,如身高、体重等。我该怎么做?

提前致谢

4

1 回答 1

1

可能有更好的方法,但这是一种便宜且容易想到的方法:

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 找到这些模块:

于 2013-11-05T03:03:50.170 回答