Next in line--CGI cookies.. I tried writing a very basic login script (which works) and tried my first attempt at setting/using cookies (which I'm still confused about). You need to set the cookies in with the HTTP header, says CGI docs, but if that's the case how can you print out a form, ask for details, THEN give them a cookie if they login? If you need to send cookies with the header, it has to already exist before the script is run the first time..that's why I'm confused.
The partial script below is trying to say (If there is a cookie, print "you're in" otherwise print the form and wait for the user to login correctly then set their cookie).
When the script is run the first time it says 'bad cookie' but even if they login correctly it says 'bad cookie'..maybe the cookie isn't being set at all. Can someone give me a hand with this?
print header, start_html;
if ( cookie('sessionID') ) {
print "You're in!";
print cookie();
}
else {
print "BAD COOKIE!<br>";
print start_form(),
table(
Tr(
td("Username:"),
td( textfield( -name => 'username', -size => '15' ) )
),
Tr(
td("Password:"),
td( textfield( -name => 'password', -size => '15' ) )
),
Tr( td( submit('send') ), ),
end_form,
);
if (param) {
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $user = param('username');
my $passold = param('password');
my $pass = md5_hex($passold); #encrypt
if ( exists $login{$user} ) {
if ( $login{$user} == $pass ) {
print "Good!<br>";
my $contents = join("::", $user, $pass);
my $cookie= cookie(
-name => 'sessionID',
-value => "$contents",
-expires => '+1h',
-secure => 1
);
print "\$contents: $contents<br>";
print "$cookie<br><br>";
print "Your cookie should have been set<br>";
}
else {
print "Bad password!<br>";
}
}
else {
print "Username not found!<br>";
}
print "You typed: $user => $pass<br>";
print "<br><br>";
foreach ( keys %login ) {
print "$_ => $login{$_}<br>";
}
}
}
"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"
sulfericacid
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.