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

Hi!

I need to send some cookies to the visitor with my CGI-script,and then to redirect him to the other site. But because of the 'cookies', that I'm sending with:
...
    print $cgi->header(-type=>"text/html",-cookie=>$cookie);
... ,
I couldn't use (after sending 'header'):
...
    use CGI qw(redirect);
...
    print redirect('http://www.newsite.com');
... ,
and that's why I tried to use:
...
    use CGI qw/:standard/;
...
    print $cgi->start_html(-head=>meta({-http_equiv =>'refresh',
 -content=>"0; url=http://www.newsite.com"}));
...,

and it works now, but I'm not sure whether this HTML TAG would work with all visitors.
So is there any better way to redirect to the new site?

Thanks!

Replies are listed 'Best First'.
Re: try to send cookie and redirect
by Ovid (Cardinal) on Apr 22, 2002 at 23:11 UTC

    wardk answered your question, but I thought I'd explain why your version worked.

    When a browser receives an "http-equiv" meta tag, it takes the information in that tag and treats it like one of the headers. Thus, your meta({-http_equiv =>'refresh'... is effectively the same as sending a redirect header.

    It's interesting to note that in some circumstances, the meta tag is preferable to a header redirect. IIS, in its last three versions, ignores a cookie that is set when you attempt to redirect, unless you swith to nph (non-parsed header) scripts. This has been a consistently annoying bug that frequently crops up. Of course, Apache doesn't have this problem :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


      It's clear for me now!
      Thank you very much!
Re: try to send cookie and redirect
by wardk (Deacon) on Apr 22, 2002 at 22:55 UTC
    This snippet works in one of my applications just fine, I think it meets your requiremeents for the most part. This is from a web-based login that redirects to the calling script.

    Create and set cookie, then redirect.

    my $cookie_name = "FOO" . $E . "SID"; my $SID_cookie = new CGI::Cookie(-name => $cookie_name, -value => $sid, -path => '/', -expires => "+2h", -domain => '.foobar.com', ); print "Set-Cookie: " . $SID_cookie . "\n"; print $q->redirect("$redirectURL");

      If you used CGI's named arguments-style, there's no need to craft your own 'Set-Cookie' header: redirect will do it for you, just like header.

      print $cgi->redirect( -uri => $new_url, -cookie => $cookie );

          --k.



      It works just fine!
      Thanks a lot!
Re: try to send cookie and redirect
by shotgunefx (Parson) on Apr 22, 2002 at 23:15 UTC
    Use redirect like you would header. It's can take all the args that header can. In my experience it works but I don't know if it's standard to take a cookie on a redirect but it has worked for me.
    print redirect(-location=>$my_url,-cookie=>[$cookie1,$cookie2],-expir +es=>'+1h' );


    -Lee

    "To be civilized is to deny one's nature."