Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

cookies/redirect problem

by ibanix (Hermit)
on Feb 25, 2003 at 20:49 UTC ( #238559=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I'm trying to write some CGI code. Need to look and see if a cookie is set; if not, set it and redirect back to the URL. Here's the piece of test code I have. When I run it, it hangs forever, so I'm assuming it's stuck in a loop.

Can anyone tell me where I went wrong?
use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); # New CGI object my $q = new CGI; my $test = $q->cookie("test"); if (defined $test) { print $q->header; print $q->start_html('Test stuff'); print "<BR><BR>\n"; print "I got cookie value: $test<BR>\n"; print $q->end_html; } else { my $cookie = $q->cookie(-name => 'test', -value => 'test value'); print $q->redirect (-cookie => $cookie, -uri => $q->self_url()); }
Thanks!

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: cookies/redirect problem
by dws (Chancellor) on Feb 25, 2003 at 21:14 UTC
    Need to look and see if a cookie is set; if not, set it and redirect back to the URL.

    You've run into a common problem, and one in which the behavior varies depending on which web server you're using.

    See IIS and Apache (which discusses a workaround), or try a Super Search for "cookie redirect".

•Re: cookies/redirect problem
by merlyn (Sage) on Feb 25, 2003 at 21:59 UTC
      I used your code as a basis for this test. I don't see what I'm doing that's very different from what you did?

      ibanix

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: (nrd) cookies/redirect problem
by newrisedesigns (Curate) on Feb 25, 2003 at 22:43 UTC

    I just tested your program on Apache/Win2000. Worked fine for me, so you're on the right track.

    Your test could use some detail; instead of redirecting, print a standard header with cookie along with a page with a self-referencing link. That way you aren't throwing it in a loop. Also, if you're using Mozilla, I suggest installing Livehttpheaders. It's great for debugging CGI scripts.

    Good luck to you.

    John J Reiser
    newrisedesigns.com

      Thanks John.

      I tried your code and it worked as expected. I got the page with the self-refrencing link; on click it shows that it found the cookie. I guess my server (IIS5.0 on Win2K) is to blame.

      dws' mentioned a thread above, and in it he stated:
      " banged my head into a wall on this one with IIS4.0 a year ago, and finally fell back on using a 0 second refresh.

      <meta http-equiv=refresh content="0;url=http://www.example.com/overhe +re.html">
      "
      Does this work for other people? Or is there a better solution?

      Thanks!

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://238559]
Approved by silent11
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2022-08-09 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?