Here's a sample CGI/Cookies script, originally taken from perlmonks (cLive).
Notice you don't need CGI::Cookie.  You may need to refresh once to see the
set cookies.  And don't tell my wife about the redheads thing, she's a
brunette. :)

#!/usr/bin/perl-w

use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $cgi = new CGI;

# grab existing %cookie
my %cookie;
for ($cgi->cookie()) {
  $cookie{$_} = $cgi->cookie($_);
}

# set a couple of cookies;
$cookie{'ID'} = 123456;
$cookie{'preferences'} = "redheads";
$cookie{'LoginName'} = "Calaban";
$cookie{'Other_cookie'}  = "whatever";

# create cookies for CGI
my @cookies;
for (keys %cookie) {
  push @cookies, $cgi->cookie( -name  => $_,
                               -value => $cookie{$_}
                             );
}

# print amended header
print $cgi->header(-cookie=>[@cookies]),$cgi->start_html(-title=>'Login');

# use our hash to check "cookie"
if(defined(my $cookieHolder = $cookie{'LoginName'})){
        print $cgi->h3("Retrieved cookie named LoginName: $cookieHolder");
}
else{
    print $cgi->h3("No cookie retrieved.");
}

# display all stored
print $cgi->h4('Stored Cookies');

for (keys %cookie) {
  print "$_ = $cookie{$_}".$cgi->br;
}


Should show:
Retrieved cookie named LoginName: Calaban
Stored Cookies
ID = 123456
preferences = redheads
Other_cookie = whatever
LoginName = Calaban

I'm learning CGI myself so if anyone else wants to pitch in please do so.

Joel

> -----Original Message-----
> From: Matthew Harrison [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, March 12, 2002 11:30 AM
> To: Stout, Joel R
> Cc: [EMAIL PROTECTED]
> Subject: Re: nearly got the cookies // debugging suggestion
> 
> 
> I have added all the debugging tools and it is still not 
> enough info to 
> sort the prob. have you got a script that retreives variables 
> that i could 
> look at?
> 
> cheers
> 
> On Tuesday 12 Mar 2002 6:48 pm, Stout, Joel R wrote:
> > A good site for beginners...
> >
> > 
> http://www.easystreet.com/~ovid/cgi_course/appendices/appendix1.html#D
> >
> > > -----Original Message-----
> > > From: Matthew Harrison 
> [mailto:[EMAIL PROTECTED]]
> > > Sent: Tuesday, March 12, 2002 10:42 AM
> > > To: [EMAIL PROTECTED]
> > > Subject: nearly got the cookies
> > >
> > >
> > > I have manager to set a cookie containing a hash of 4 keys
> > > and 4 values.
> > > This is the script i have to retrieve the cookies and modify
> > > the look of
> > > the page using them. i have constructed it using bit frmo
> >
> > www.perldoc.org.
> > it keeps saying it cannot run because of something in the way i am
> > retieving the cookies. what is wrong?
> >
> > #!/usr/bin/perl
> >
> > use CGI qw/:standard/;
> > use CGI::Cookie;
> >
> > %cookies = fetch CGI::Cookie;
> > $font = $cookies{'font'}->value;
> > $size = $cookies{'size'}->value;
> > $color = $cookies{'cookie'}->value;
> > $back = $cookies{'back'}->value;
> >
> > print header();
> >
> > print <<END_OF_HTML
> >
> > <html>
> > <body bgcolor="$back" textcolor="$color">
> > <p><font face="$font" size="$size">
> > hello people, hope you like your prefs.
> > </font></p>
> > </body>
> > </html>
> >
> > END_OF_HTML
> 
> -- 
> Matthew Harrison
> Internet/Network Services Administrator
> Peanut-Butter Cheesecake Hosting Services
> Genstate
> www.peanutbuttercheesecake.co.uk
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to