in reply to Cookies problem
Hi Vorteks, welcome to the monastery!
Well, first and foremost, always use strict and warnings. You'd be surprised how many silly typos they catch, as well as making your code cleaner. And, when your CGI's are wonderfully huge applications that need a speed boost, you'll have a much easier time moving to mod_perl.
Okay, that said, looks like you're setting the cookie okay, but not with any value. Notice your single quotes around the scalar $user when setting the cookie's value. Those quotes are non-interpolative, in other words what you're setting is the string "$user", not the variable's contents. You don't actually need quotes at all in this case.
Next, I'm going to jump a head a little. Cookies are name/value pairs, and generally you only want to use one cookie on a web page, so as not to be a pain. So it's best to use a hash. You can then pass a reference to your cookie hash to the cookie() function, and thus get more than one hunk of data into a cookie.
Try some code like this:
#!/usr/bin/perl use CGI qw/:standard/; use strict; use warnings; #Place the user's existing cookie in a hash, if they have one my %userPrefs = cookie('prefs'); #set the user $userPrefs{user} = 'user1'; # No need to double-quote this string # let's set some timestamps for fun $userPrefs{accessed_last} = $userPrefs{accessed}; $userPrefs{accessed} = scalar localtime; # Now we get a new cookie object my $cookie = cookie( -name => 'prefs', -value => \%userPrefs, -expires => '+1h', ); # and pass it to the browser print header(-cookie=> $cookie); # and let's print the latest cookie info print map {"$_ : $userPrefs{$_}<br>"} sort keys %userPrefs;
The above code is typed off the cuff, and is untested for typos...
cheers!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Cookies problem
by vorteks (Acolyte) on Aug 03, 2002 at 06:48 UTC | |
by sedhed (Scribe) on Aug 03, 2002 at 16:53 UTC | |
by Phaysis (Pilgrim) on Aug 05, 2002 at 07:09 UTC | |
Re: Re: Cookies problem
by Anonymous Monk on Aug 04, 2002 at 17:43 UTC | |
by vorteks (Acolyte) on Aug 04, 2002 at 17:48 UTC | |
by vorteks (Acolyte) on Aug 04, 2002 at 18:28 UTC |