cookie后,HTTP重定向不起作用

时间:2015-04-17 22:49:02

标签: perl http redirect cookies header

将Perl与CGICGI::Session

一起使用
 $session = CGI::Session->load();
 $session->header(-cookie => $cookie, @_);

然后在上面:

 $q = new CGI;
 $q->redirect('url.com');

重定向不会以这种方式工作,有解决方案吗?我不想使用HTML <meta http-equiv="refresh">元素

2 个答案:

答案 0 :(得分:0)

现在,您可以使用$session->redirect ...

#!/usr/bin/perl --
use strict;
use warnings;
use Data::Dump qw/ dd /;
use CGI::Session;
sub CGI::Session::redirect {
    my $self = shift;
    my( @args ) = 1==@_ ? (  -uri => @_ ) : @_;
    return $self->query->redirect(-cookie => $self->cookie, @args );
}

dd( CGI::Session->new->redirect );
dd( CGI::Session->new->redirect( 'http://blah' ) );
dd( CGI::Session->new->redirect( -status => '307 Temporary Redirect' ) );
__END__
"Status: 302 Found\r\nSet-Cookie: CGISESSID=ca91ba2e503366755dfad156fc2c3db8; path=/\r\nDate: Sat, 18 Apr 2015 01:15:49 GMT\r\nLocation: http://localhost\r\n\r\n"
"Status: 302 Found\r\nSet-Cookie: CGISESSID=be18ccf65a3cfaa4997b7047d662a825; path=/\r\nDate: Sat, 18 Apr 2015 01:15:49 GMT\r\nLocation: http://blah\r\n\r\n"
"Status: 307 Temporary Redirect\r\nSet-Cookie: CGISESSID=ae059a7d25d0932077035bbc108784f2; path=/\r\nDate: Sat, 18 Apr 2015 01:15:49 GMT\r\nLocation: http://localhost\r\n\r\n"

答案 1 :(得分:0)

问题:

您正在输出两个标题。

解决方案:

指示$ session-&gt;标头输出正确的标头。

$session->header( -status => '302 Found', -location => $url );

  • $session->header是以下的快捷方式:

    my $cookie = CGI::Cookie->new( -name => $session->name, -value => $session->id );
    print $cgi->header( -cookie => $cookie, @_ );
    
  • $cgi->redirect($url)是以下的快捷方式:

    $cgi->header( -status => '302 Found', -location => $url );