in reply to How do they expect cookies to work (like this)??

I think what you're saying is that you want to be able to access the cookie you are about to set in the current invocation???

If so, you need to set up a var to track this "cookie" (it's not a cookie before it's set, hence the quotes).

#!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new(); our $cookie = ''; if ( defined $q->cookie('cookiename') ) { $cookie = $q->cookie('cookiename') # validate cookie here based on whatever } elsif ($q->param('some_field_name') { # form submitted $cookie = 'whatever'; } else { # show the html form }
Then, whenever you print a page, ensure the cookie gets sent in the header.

I think understanding exactly what it is you don't understand is more the problem here than the actual problemn. perhaps you can explain it in a bit more detail?

.02

cLive ;-)

ps - you might want to look at the concept of using a "cookie jar" in your script. That way you can seamlessly ensure cookies get sent when you print headers. Here's a snippet....

# at beginning of script use CGI; our @cookie_jar = (); our $q = CGI->new(); # when you want to set a cookie push @cookies, $q->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h'); # when you print a page, instead of calling $q->header # use this instead print cookie_header; # which sends all cookies sub cookie_header { return $q->header(-cookie=>\@cookie_jar); } # if you're on mod_perl, you also need to empty # the cookie jar here, so amend sub cookie_header { my header = $q->header(-cookie=>\@cookie_jar); @cookie_jar = (); return $header; }
But, like I said, I'm grasping at straws here. I think if you could explain what you don't understand though, you'd probably find the answer yourself :)

The key to successful programming is to understand the questions. Once you've done that, the answers are trivial :)

.02

cLive ;-)

Replies are listed 'Best First'.
Re: Re: How do they expect cookies to work (like this)??
by Anonymous Monk on Oct 01, 2003 at 20:52 UTC
    Ok, I will try to re-ask my question so you can better understand what I am aiming for.

    I want an HTML form (which asks for a username and password) to redirect back to itself and with the user/pass make a cookie. When the cookie is sent to header after it's detected, it will access semi-secure information and won't print the form anymore.

    In short, I want the form to make the cookies when it returns to itself after the user information is supplied.

    I have printed your snippets out and will be looking them over more extensively after work today but there are a few things I can honestly say I haven't seen before. For example the use of our.

    Thanks for your help.

      #!/usr/bin/perl use strict; use warnings; use CGI; our $q = CGI->new(); our @cookie_jar=(); our $secure_cookie = defined $q->cookie('securecookie') ? $q->cookie('securecookie') : ''; # is a valid cookie set? if ($secure_cookie) { if ( cookie_ok($secure_cookie) ) { show_secure_stuff(); } else { # invalid cookie, so delete and show form push @cookie_jar, $q->cookie( securecookie => '' ) show_form(); } } # have they just completed the form elsif ($q->param('username')) { check_login_combo(); } # first run, so show form else { show_form(); } sub show_form { # display login form here # with fields 'username' and 'password' } sub check_login_combo { # based on username, grab correct crypted password # from db/textfile and store in $stored_cryptpass # do various other error checks and re-present form # if invalid input my $user = $q->param('username'); my $pass = $q->param('password'); if ($stored_cryptpass eq crypt($pass,$stored_cryptpass) { push @cookie_jar, $q->cookie({-name=>'secure_cookie', -value=>"$user:$stored_cryptpass"}); show_secure_stuff(); } else { # bad login show_form(); } } sub cookie_ok { # do similar check on cookie my ($user,$cryptpass) = split ':', $_[0]; # grab stored_cryptpass and check return $cryptpass eq $stored_cryptpass ? 1 : 0; } sub show_secure_stuff { # display secure page here - ensuring you send the cookie print $q->header(-cookie => \@cookie_jar). 'rest of page here'; }

      .02

      cLive ;-)