Mark Weaver wrote:

> Hi All,
> 
> 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.

First of all, you should never code without a 'use strict' at the top
of each file.  I made some adjustments and combined the two cookie values into
one using '::' separator (rather than using two cookies).  Tested minimally
only at the commandline.

#!/usr/bin/perl -w --

use strict;
require "updater_cookie.pm";

# the below code can be done using CGI parser more readily:

my %FORM;
read STDIN, my $buffer, $ENV{'CONTENT_LENGTH'} || 27;
my @appdata = split /&/, $buffer;

# Do a little quick decoding of all the form data so we can use it
foreach my $appdata (@appdata) {
        my ($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

my $inUser = $FORM{'userName'};
my $inSecret = $FORM{'secret'};

# sending the username/password combo to the setCookie sub
# to create and store the cookie information.

setCookie ($inUser, $inSecret);

my ($username, $passwd) = getCookie ();
if ($username) {

        print <<EOD;
<html>
<head><title></title>
</head>
<body bgcolor="#ccddee">
<BR><BR><BR><BR>
<div align=center>
<h3>
This is the information you input.<br>
The username was: $username.<br>
The password was: $passwd.<p>
This is the end of the cookie Test.
</h3>
</div>
</body>
</html>
EOD

} else {

        print <<EOD;
<html>
<head><title></title>
</head>
<body bgcolor="#ccddee">
<BR><BR><BR><BR>
<div align=center>
<h1>
There weren't any cookies for me to retrieve.
</h1>
</div>
</body>
</html>
EOD

}
exit;

__END__


use strict;
use CGI qw/:standard/;
use CGI::Cookie;

# create and output the cookie

sub setCookie {
        my ($user, $pass) = @_;

my $cookie = new CGI::Cookie(-name => 'log_in', -value => "$user::$pass",
  -secure => '1');

# you could use the same CGI object that you *should* use to parse the form
# data here:

my $query = new CGI;
print $query->header(-cookie=>$cookie);

}

# retreive the cookie information

sub getCookie {

my %cookies = parse CGI::Cookie ($ENV{HTTP_COOKIE});
if (exists $cookies{'log_in'}) {
        my $login = $cookies{'log_in'}->value;
        return split /::/, $login;
}
return (undef, undef);

}

1;

__END__


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to