I suspect that you're having difficulty understanding how CGI works. Anything you want the server to send to the browser has to be printed to STDOUT. In the case of cookies these must be included in the headers that are printed before any html output is sent. Which is why grep suggested:
print $query->header(-cookie=>$cookie);
I hope the follow example works for you and helps you understand cookies better:
#!/usr/bin/perl -wT use CGI; use strict; my $cgi = new CGI; # Use CGI in an OO sense my $header; my $message = ""; if($cgi->param("name")) # Set the cookie if we have a name { # Only take nice characters from the name my ($name) = ( $cgi->param("name") =~ /([\w -]+)/ ); # Create our cookie my $cookie = $cgi->cookie({-name=>"name", -value=>$name}); # Add our cookie to the header $header = $cgi->header({-cookie=>$cookie}); # Some message $message = "Thanks for visiting us $name."; } elsif(my $name = $cgi->cookie("name")) # Read name from cookie (if it + exists) { # Greet name $message = "G'day $name. It's nice to see you again"; } $header ||= $cgi->header(); # (first time, or already has cookie) # Output html print $header, "\n", $cgi->start_html(), "\n", $message, "\n", $cgi->start_form(), "\n", "You can add your name here:", "\n", $cgi->textfield({-name=>"name", -value=>"", -override=>1}), "\n", $cgi->submit({-value=>"Greet me!"}), "\n", $cgi->end_form(), "\n", $cgi->br(), "\n", $cgi->start_form(), "\n", $cgi->submit({-value=>"I'm back!"}), "\n", $cgi->end_form(), "\n", $cgi->end_html(), "\n";
Of course I'm not using CGI::Cookie here, but it should illustrate the point regardless. If you want to try this script out you can visit http://perltraining.com.au/~jarich/cgi-bin/cookietest.cgi.

Not having used CGI::Cookie before I don't think I can comment much on it. I can mention that some of your problems may be in the use of "H" instead of "h" in your expiry date. I know that this caused me some problems presumably because the browser expired my cookie very quickly...

The following functions:

#!/usr/bin/perl -wT use CGI; use CGI::Cookie; use Data::Dumper; use strict; my $cgi = new CGI; setcookie("fish", "fosh"); getcookie(); sub setcookie { my ($DBuser, $DBpasswd) = @_; my $cookie1 = new CGI::Cookie(-name=>'DB', -value=>{ username => $DBuser, password => $DBpasswd, }, -expires => '+1h'); print $cgi->header(-cookie=>[$cookie1]); } sub getcookie { my %cookies1 = fetch CGI::Cookie; return unless %cookies1; my $db = $cookies1{'DB'}; return unless $db; my %dbvs = $db->value if $db; print Dumper(%dbvs); print Dumper(%cookies1); }
work great for me.

Hope it helps.

jarich

Update: Had a revelation about H vs h and finally made the functions work consistently.
Update2: It occurs to me that you may feel that printing the cookie to the screen is a security risk because passwords may be involved. Whilst this is true I hope you realise that any passwords you store in the cookie will be visible to the owner of the cookie (if they want to see them) as well as to anyone who can see the environment variables of the browser process. What is more the cookie will be passed to all other websites in your domain. So, for example, if I pass out a cookie to the www.unimelb.edu.au domain, then it's not just my cgi scripts that can access that cookie, but every website over the whole campus. (Extrapolate for something like geocities...) As a general rule, storing passwords in a cookie is a really bad idea. Storing anything that you don't want the user to be able to read and record is just asking for trouble.


In reply to Re: CGI::Cookie issues by jarich
in thread CGI::Cookie issues by sdyates

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.