Fellow monasterians,
I've managed to survive many scenarios where I've had to write multiple CGI headers in a script, but it always feels like I'm hunting gophers--as soon as I kill one, another pops up. And now I'd like to settle the issue once and for all. Note: In all fairness, I did ask a related question, but that was concerned more with running other Perl programs.
Anyway, I use a combination of HTML::Template and CGI to populate my pages, write cookies, and to redirect. Of course, all these require headers. Sure it would be convenient to group them and only write it once, but in larger programs, where there can be many conditions, it's often just not possible. Here is a stripped down version of a larger application that highlights the problem:
use CGI; my $query = new CGI; if ($somecondition) { cookie(); } if ($anothercondition) { menu(); } else { my $template = HTML::Template->new(filename => 'some.tmpl'); $template -> param(message => "Hello World"); print "Content-type: text/html\n\n"; print $template->output(); } exit(0); sub cookie { my $newcookie = $query->cookie( -name => 'sessionID', -value => "test", -expires => '+60m'); print $query->header(-cookie => $newcookie); } sub menu { print $query->redirect('http://www.somedomain.org/index.html'); }
In some cases I only need to print one, other times all three. Is there an alternative to writing the bloated code to jump through all the hoops to eliminate the problems that arise, e.g., the 302 error. Thanks!
Update 1: I think I confused the issue with my code. It boils down to, how do I 1) print a cookie, and 2) redirect in the "same breath?"
print $query->header(-cookie => $newcookie); print $query->redirect('http://www.somedomain.org/index.html');
Update 2: FWIW, I have finally managed to write the cookie and redirect at the same time, but without using CGI conventions. Sadly. But it works and I'm back on track.
print "Set-Cookie: $cookie\n"; print "Location: $path\n\n";
Update 3: Smylers came up with the CGI solution I was looking for. Thanks.
In reply to The problems of multiple CGI headers by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |