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!


In reply to Re: Cookies problem by sedhed
in thread Cookies problem by vorteks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.