motasem1973 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am trying to use both the Set-Cookie and Location headers together in my script to, first, store my cookie and then redirect execution to another script. The code used is the following:
print "Set-Cookie: $cookie","\n"; print "Location: /cgi-bin/enquiry.pl","\n\n";

The result is that the cookie is stored successfully BUT the redirection is not working (the browser is showing a white screen without output) (The redirection header when used alone is working). Where could the problem be? any way around the problem?

Help is highly appreciated,
Motasem

Replies are listed 'Best First'.
Re: problem using the set-cookie and the Location headers together
by shmem (Chancellor) on Sep 03, 2008 at 20:23 UTC

    Use CGI. Use CGI!

    my $q = CGI->new; my $cookie = $q->cookie ( -name => 'PLAETZCHEN', -value => Digest::MD5::md5_hex(time.{}.rand().$$), ); print $q->redirect (-cookie => $cookie, -uri => $url);

    and use an absolute URL.

Re: problem using the set-cookie and the Location headers together
by SFLEX (Chaplain) on Sep 03, 2008 at 19:56 UTC
    you have to redirect before you store a cookie.
Re: problem using the set-cookie and the Location headers together
by Anonymous Monk on Sep 04, 2008 at 08:06 UTC
Re: problem using the set-cookie and the Location headers together
by toolz (Initiate) on Nov 30, 2012 at 19:21 UTC
    I recently discovered this trick to accomplish what you want:
    print "Set-Cookie: foo=blah; path=/\n"; print "Refresh: 0; URL = /cgi-bin/enquiry.pl\n"; print "Content-type: text/html\n\n";