Click to See Complete Forum and Search --> : retrieving cookies with CGI::Cookie


pccode
09-24-2005, 08:43 PM
I'm currently using the code below to retrieve specific information from a cookie.


use CGI::Cookie;
%cookies = fetch CGI::Cookie;
my $isset = $cookies{'user'};
my @temp1 = split(/\;/, $isset);
my @temp2 = split(/=/, $temp1[0]);
my $username = $temp2[1];


I don't have a lot of experience with perl, but I'm sure there's an easier way to retrieve a cookie value. The code above is used to retrieve a cookie named user. Then I need to retrieve the value from that cookie named username. Unfortunately the only way I know how to do this is by splitting the $isset variable. Any suggestions?

Nedals
09-25-2005, 10:08 AM
use strict;
use CGI;

my $q = new CGI;
my $username = $q->cookie('username');

UPDATE:
Fixed very strange typo :)

CyCo
09-25-2005, 06:39 PM
Hey, Nedals...surely you meant to write:my $q = new CGI; ;)

Nedals
09-25-2005, 07:29 PM
my $q c= new CGI;

Where did that come from? :confused:
Thanks!

CyCo
09-25-2005, 08:07 PM
Welcome!
I swear when I looked at your post earlier today, I didn't recall seeing that "c" in there...Very strange!...
Twilight Zone :eek:

pccode
09-28-2005, 03:12 PM
A much better way to retrieve a cookie.:) Thanks for your help.