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! |