• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

cgi cookie with multiple names / values

I create a cookie and read it and print it's value something like this.

use CGI::Cookie

my $salt_key=&gensalt($salt_length);
my $sess_key = unix_md5_crypt(rand(11),$salt_key);

my $c = new CGI::Cookie(-name => 'sess',
-value => $sess_key,
-domain => $cook_domain,
-path => $cook_path,
-secure => 0
);

print "Set-Cookie: $c\n";

%cookies = fetch CGI::Cookie;
print  $cookies{'sess'};

I would like to know the best method to include other values in the same cookie like

user=bob
key=1

Once the additional values are in the cookie are the read and printed the same way or is 1 fetch all I need?

print  $cookies{'user'};
print  $cookies{'key'};

is it better to make a second cookie or is all data in one cookie acceptable?
0
Bob-Villa
Asked:
Bob-Villa
1 Solution
 
Adam314Commented:
You can either create multiple cookies, or one cookie with multiple values.  Either way will work.  To create one cookie with multiple values:

my $c = new CGI::Cookie(-name => 'sess',
-value => [$sess_key, "Value2", "Value3", "Value4"],
-domain => $cook_domain,
-path => $cook_path,
-secure => 0
);

Then to retrieve the values:
@values=$cookies{'sess'};


Based on the name of your cookies, it looks like you are creating sessions.  If so, you might want to look at the CGI::Session module:
http://search.cpan.org/~markstos/CGI-Session-4.20/lib/CGI/Session.pm
0
 
Bob-VillaAuthor Commented:
Wanted to avoid loading another module. WIth only a few lines of code you can emulate CGI::Session without all of the weight.

@values=$cookies{'sess'}; did the trick. Not sure how I missed that.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now