0

Cookie で 1 つの値 (変数) を渡す方法は知っていますが、1 つの 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 に答える