cgi cookie with multiple names / values

Posted on 2007-07-27
Last Modified: 2013-12-25
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


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?
Question by:Bob-Villa
    LVL 39

    Accepted Solution

    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:

    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:
    LVL 1

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now