webstudioro has asked for the wisdom of the Perl Monks concerning the following question:

I write this script for my member area (login system)
but don't work all script. Please test the script and
tell me where is the problem. Thank you.

#!perl BEGIN { $| = 1; open (STDERR, ">&STDOUT"); print qq~Content-type: text/html\n\n~; } use warnings; use CGI qw(:standard); use CGI::Cookie; use CGI; $user = "test"; $pws = "123456"; $query = CGI->new(); if ($query->param('user') eq ""){ print "<font color=\"red\">Enter your User name.</font>"; exit; } if ($query->param('pws') eq ""){ print "<font color=\"red\">Enter your password.</font>"; exit; } if (($query->param('user') eq "$user") && ($query->param('pws') eq "$p +ws")) { $query = CGI->new(); my %cookie = CGI::Cookie->fetch; if (exists $cookie{'$user'}) { print header, start_html('got cookie'), p('found the cookie: ' . $cookie{$user}->value), end_html, ; exit; } else { my $cookie = CGI::Cookie->new(-name=>$user,-value=>$pws,-expires=>' ++23h'); print header(-cookie=>$cookie), start_html('need cookie'), p('setting cookie, please reload'), end_html, ; exit; } } print "Your User name / Password it's not good.<br>"; exit;

Replies are listed 'Best First'.
Re: Cookie problem
by Thelonius (Priest) on Aug 14, 2003 at 14:22 UTC
    This line is a big problem:
    print qq~Content-type: text/html\n\n~;
    You have already printed the header before the rest of your script runs, so any cookie is going to go into the body of the response, where it will do no good.
      ok. I delete the problem line (thank you)
      and work very good , but the cookie it's not
      work (customize), why?

        Variables don't interpolate in single quotes:

        if (exists $cookie{'$user'}) {

        This code quotes a lot of variables unnecessarily; but this is definitely a bug. Also, you're recreating the $query object; I'm not sure if that'll wipe out any cookies, but it's definitely unnecessary.