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 ;-) |