How to set a cookie and redirect

J

James

I know this is not the right topic to post here, but I get better
responses, so here it is again.
In a cgi script, I am trying to set a cookie, then redirect to a
different url.

use CGI;
my $q = new CGI;
my $c = $q->cookie( -name => 'ID', -value => 'bob');
print $q->header(-type => "text/html", -cookie => $c);
print $q->redirect(-url=>"index.cgi");

It does not work.

Thanks in advance.

James
 
U

usenet

James said:
I know this is not the right topic to post here,

you're right about that.
but I get better responses, so here it is again. In a cgi script, I
am trying to set a cookie, then redirect to a different url.

Usually you just get flamed (or ignored, or killfiled) for posting OT
in this group. However, I have particular sympathy for this question
because I had to do the same thing myself, and it took me HOURS to
figure it out, so at the risk of offending the group (by providing the
answer to an OT reply), here's the code you need:

my $cookie = cookie(-name => 'foo', -value => 'bar');
print redirect(-uri => "http://whatever.com", -cookie => $cookie);

Cheers.
 
K

Keith Keller

use CGI;
my $q = new CGI;
my $c = $q->cookie( -name => 'ID', -value => 'bob');
print $q->header(-type => "text/html", -cookie => $c);
print $q->redirect(-url=>"index.cgi");

It does not work.

"It does not work" is a poor problem description. Explain specifically
what you want and what you see.

In this case, the problem is patently obvious: you didn't read the CGI
docs for using redirect. Read them, fix your code, and post back with
more specific information if it still doesn't work.

--keith
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
you're right about that.

(Emphasis mine).

You're just perpetuating the cycle, ain't ya?

Sinan
 
U

usenet

A. Sinan Unur said:
(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
[a helpful reply to an OT question]
You're just perpetuating the cycle, ain't ya?

Yeah. Sorry. I'm totally out of control. But, really, a question about
CGI.pm usage is not _that_ OT in a group called comp.lang.perl.misc, is
it? It is, after all, a core module now. And the OP does have a point
that the groups dedicated to CGI are not always very helpful.
 
A

A. Sinan Unur

A. Sinan Unur said:
(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
[a helpful reply to an OT question]
You're just perpetuating the cycle, ain't ya?

Yeah. Sorry. I'm totally out of control. But, really, a question about
CGI.pm usage is not _that_ OT in a group called comp.lang.perl.misc, is
it? It is, after all, a core module now. And the OP does have a point
that the groups dedicated to CGI are not always very helpful.

In that case, reading the documentation would have helped both of
you save some time:

http://search.cpan.org/~lds/CGI.pm-3.11/CGI.pm#GENERATING_A_REDIRECTION_HEADER

You should always use full URLs (including the http: or
ftp: part) in redirection requests. Relative URLs will
not work correctly.

Sinan
 
U

usenet

Keith said:
In this case, the problem is patently obvious: you didn't read the CGI
docs for using redirect.

FWIW, the CGI docs don't mention (or even hint) that redirect may be
used WITH a cookie. In Lincoln Stein's book, "Official Guide to
Programming with CGI.pm," he does mention that "All named arguments
recognized by header() are also recognized by redirect(). However, most
HTTP headers, including those generated by -cookie and -target, are
ignored by the browser." (page 225).

That "cookie caveat" may have been true in 1998, when my book was
published, but I have not encountered a reasonably modern browser that
did not accept a redirect with a cookie.

It was only by reading this warning in the book that I ever realized it
was possible to put a cookie in a redirect. I have never seen this
documented by Dr. Stein anywhere else other than indirectly by this
brief warning in the book.

I think the OP may be forgiven for not having inferred this
functionality from the sparse information in the perldoc.
 
U

usenet

A. Sinan Unur said:
In that case, reading the documentation would have helped both of
you save some time:

Oh, I hadn't even noticed the OP's relative URL. The OP's question
centered on the question of how to drop a cookie AND do a redirect - I
was responding only to that aspect of the question (though I did, at
least, show a properly scoped URL in my example). The technique for
redirection with a cookie is not well documented (in fact, I cannot see
that it is documented at all in perldoc). I found only an indirect
reference to the technique in Stein's CGI.pm book.
 
G

Gunnar Hjalmarsson

Keith said:
"It does not work" is a poor problem description. Explain specifically
what you want and what you see.

In this case, the problem is patently obvious: you didn't read the CGI
docs for using redirect.

I disagree on that problem description. The OP has a problem because he
doesn't know enough about CGI and HTTP.

The OP should be advised to learn the basics about CGI and HTTP before
trying to write a CGI app. Then, but only then, CGI.pm may or may not
provide useful tools.

Unfortunately, in my experience, when you have learned the CGI and HTTP
basics, you often find that CGI.pm offers little but a redundant level
of abstraction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top