in reply to Storing an array in a cookie...
What you're trying to do is maintain state in an inherently stateless protocl (HTTP). To get around this, typically the user is given a a sessionID and this is tied to state information that is stored in a database. That's a more robust solution. For something quick 'n dirty, you could use Storable to serialize your data structure, store it in a cookie, and later retrieve it and deserialize it. Here's a small code sample to show you how this is done (minus using a cookie):
use strict; use warnings; use Storable qw/ freeze thaw /; use Data::Dumper; my %foo = ( one => [ qw/ a b c / ], two => [ qw/ 1 2 3 / ] ); my $serialized = freeze \%foo; my %clone = %{ thaw $serialized }; print Dumper \%clone;
For the snippet above, you would store $serialized in the cookie. I've never actually tried this, but it should work. However, remember that this is not a good long-term solution. Amongst other things, cookies accept a max of 4K of data and will truncate anything over that. If you rely on this technique too much, you'll get bit. Further, this increases bandwidth (slightly) and setting too many cookies (by overusing this technique) will also cause problems.
Good luck!
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|