I know this appears simple enough, but its really got my shorts in a bunch. The problem, in short, is this; I need to create a cookie with login information stored in the cookie and then I need to retrieve the cookie for in-process authentication purposes.
PROCESS:
1) when user initially logs in, receive login info 2) check database for correct login or no 3) if correct pass user through, create cookie and store. 4) Cookie expires at end of session.
MID-PROCESS:
while the user is in-session the cookie, named 'log_in' will need
to be accessed in order to pass authentication info back to the main program which uses it to access a backend database to extract or insert information.
THE PROBLEM:
the main program is a single script that uses routines stored in seperate modules local to the main program. ( All the routines, at one time lived in the same script, but that just got too messy as the program grew. ) The module which controls the creation of the cookie being set is in a module by itself. It creates the cookie just fine when the sub "setCookie" is called. However, if I attempt to test for the existance of the cookie, i.e.
if ( getCookie() ) { # extract athen info from cookie # send authen info to db }
If the cookie exists then testing the getCookie() sub should return a value of "1". such as doing it like this:
$testValue = getCookie(); if ( $testValue == 1 ) { # the cookie exists # perform some function } else { # $testValue == 0 # the cookie doesn't exist }
Since the cookie does exist when testing the getCookie sub $testValue should == 1. however, it doesn't, ergo I can never retrieve my stored cookie. ( I know the cookie exists because I can check with the browser, find the cookie and view the current values being stored in the cookie, which turn out to be the current and correct values. )
So, my question is, considering the code in the three programs below, cookieTest.cgi, updater_cookies.pm, newCookie.cgi, what am I doing wrong in the first two programs and why does it only work correctly with the third program?
PROGRAM ONE: COOKIE TEST.CGI #!/usr/bin/perl
require "updater_cookie.pm";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @appdata = split(/&/, $buffer);
# Do a little quick decoding of all the form data so we can use it foreach $appdata (@appdata) { ($name, $value) = split(/=/, $appdata); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }
# username/password coming in from form on cookieTest.html # ######################################################## $inUser = $FORM{'userName'}; $inSecret = $FORM{'secret'};
# sending the username/password combo to the setCookie sub # to create and store the cookie information. &setCookie($inUser, $inSecret);
if ( getCookie() ) { &getCookie; # cookie info should be returning in the form # of two variables: $username & $passwd print "<html>\n"; print "<head><title></title>\n"; print "</head>\n"; print "<body bgcolor=\"#ccddee\">\n"; print "<BR><BR><BR><BR>\n"; print "<div align=center>\n"; print "<h3>\n"; print "This is the information you input.<br>\n"; print "The username was\: $username.<br>\n"; print "The password was\: $passwd.<p>\n"; print "This is the end of the cookie Test.\n"; print "</h3>\n"; print "</div>\n"; print "</body>\n"; print "</html>\n"; } else { print "<html>\n"; print "<head><title></title>\n"; print "</head>\n"; print "<body bgcolor=\"#ccddee\">\n"; print "<BR><BR><BR><BR>\n"; print "<div align=center>\n"; print "<h1>\n"; print "There weren\'t any cookies for me to retrieve.\n"; print "</h1>\n"; print "</div>\n"; print "</body>\n"; print "</html>\n"; } exit;
PROGRAM TWO: UPDATER_COOKIE.PM #!/usr/bin/perl
use CGI qw/:standard/; use CGI::Cookie;
# responsible for creating the cookie # ############################################################### sub setCookie { my $cookie = new CGI::Cookie(-name => 'log_in', -value => [$inUser, $inSecret], -secure => '1', );
my $query = new CGI; print $query->header(-cookie=>$cookie); }
# sub routine added as afterthought in order to quickly check # for the existance of cookie. this sub is much like the appendix. # isn't good for much and is redundant. sub checkCookie { my %Check = parse CGI::Cookie($ENV{HTTP_COOKIE}); if ( %Check ) { return 1; } else { return 0; } }
# responsible for retreiving the stored cookie information # ############################################################### sub getCookie { my ($username, $passwd) = ""; my %cookies = parse CGI::Cookie($ENV{HTTP_COOKIE}); if (%cookies) { if ($cookies{'log_in'}) { ($username, $passwd) = $cookies{'log_in'}->value; $id = ($username, $passwd); return ($username, $passwd); } } } 1;
PROGRAM THREE: NEW COOKIES.CGI
#!/usr/bin/perl
use CGI qw/:standard/; use CGI::Cookie;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @appdata = split(/&/, $buffer);
# Do a little quick decoding of all the form data so we can use it foreach $appdata (@appdata) { ($name, $value) = split(/=/, $appdata); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }
$inUser = $FORM{'userName'}; $inSecret = $FORM{'secret'};
my $cookie = new CGI::Cookie(-name => 'log_in', -value => [$inUser, $inSecret], -secure => '1', );
my $query = new CGI; print $query->header(-cookie=>$cookie); my ($username, $passwd) = ""; my %cookies = parse CGI::Cookie($ENV{HTTP_COOKIE}); if (%cookies) { if ($cookies{'log_in'}) { ($username, $passwd) = $cookies{'log_in'}->value; print "<html><head><title>My Cookie Test</title></head>\n"; print "<body bgcolor=\"#ccddee\">\n"; print "<BR><BR><BR><BR>\n"; print "<div align=center>\n"; print "<h1>This is a cookie test<p>\n"; print "The Values entered were\:<p>\n"; print "Username\: $username Password\: $passwd<p>\n"; print "End of COOKIE test.\n"; print "</div>\n"; print "</body\n"; print "</html>\n"; } }
thanks a ton for the help,
-- Mark
"If necessity is the mother of invention, then who's the father?" ----------------------------------------------------------- Paid for by Penguins against modern appliances(R) Linux User Since 1996 Powered by Mandrake Linux 8.2 & 9.1 ICQ# 27816299
_______________________________________________ Perl-Unix-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs