【问题标题】:Passing multiple values in perl cookie在 perl cookie 中传递多个值
【发布时间】:2013-11-15 19:50:24
【问题描述】:

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

发送:

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

阅读:

$name = cookie('name');

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

提前致谢

【问题讨论】:

    标签: perl cookies cgi send


    【解决方案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 上找到这些模块:

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2018-11-08
      • 2022-10-14
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多