sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

I've read so many tutorials/docs on cookies and CGI::Cookies but I still can't figure out how (once a cookie is set) extract the value so I can use it later on during the script.

Test print of cookie -- Cookie: sessionID=test%3A%3Atest%3Ad41d8cd98f00b204e9800998ecf8427e; p +ath=/; expires=Thu, 07-Aug-2003 22:20:06 GMT
My cookie code (which finally works)-- $cookie = cookie(-name => 'sessionID', -value => makeCookieValue($userpass), -expires => '+1h' );
I've tried this suggestion which someone gave to me-- my $cookievalue = getFirstOrValue(cookie()); print "New cookie stuff: $cookievalue";
After the user is logged in, I want to be able to take the cookie valye into a scalar so I can split and use the $user and $pass (After they sign in, the page needs to be personalized with their username displayed somewhere, just like on top of this page it says 'log sulfericacid out'.

Thanks for your help everyone.

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Extracting the cookie value
by Mr. Muskrat (Canon) on Aug 07, 2003 at 21:38 UTC

    In the CGI docs (and also the docs for CGI::Cookie), once you get past the part about setting cookies, it tells you how to read cookies.

    CGI:

    use CGI; my $query = new CGI; my $sessionid = $query->cookie('sessionID');

    CGI::Cookie:

    use CGI qw/:standard/; use CGI::Cookie; my %cookies = fetch CGI::Cookie; my $sessionid = $cookies{'sessionID'}->value;

Re: Extracting the cookie value
by MrCromeDome (Deacon) on Aug 07, 2003 at 21:56 UTC
    "After the user is logged in, I want to be able to take the cookie valye into a scalar so I can split and use the $user and $pass. . . "

    Please tell me I'm misunderstanding your intentions. You're not going to store a user name and password in your cookie, are you? If so, let me visit your site with my friend JavaScript ;) You'd be much better served by storing a unique (and very hard to guess) session ID in the user's cookie, then looking up information about that session from something on the server (database, file, etc.). Much more efficient, much more secure.

    See Mr. Muskrat's code for how to read the cookie properly. Excellent suggestion, ++ to him :)

    Cheers!
    MrCromeDome

      You're not going to store a user name and password in your cookie, are you?

      You realize thats exactly how perlmonks does it? see?
        Yeah :) Doesn't mean it's the best way of doing it though ;) I didn't pay attention to what he was using his app for, but it frankly doesn't strike me as being the best way of handling logins regardless of when/where it is used.

        Of course, TIMTOWTDI! :) That's just the rather biased opinion of this Perl Monk ;)

        Cheers!
        MrCromeDome