无论我向expires()
或expires_delta()
提供的到期值如何,Cookie到期时间始终为一小时。如何更改它以使会话和cookie到期时间匹配?
答案 0 :(得分:2)
虽然我喜欢vti的作品,但这种发行看起来过时了,并且在过去被取代了。今天,Mojolicious::Sessions:
解释了设置会话到期日期的标准方法default_expiration
my $time = $sessions->default_expiration; $sessions = $sessions->default_expiration(3600);
会话的默认时间从现在开始以秒为单位,默认为
3600
。每个请求都会刷新到期超时。将值设置为0将允许会话持续到浏览器窗口 虽然已关闭,但这可能会产生安全隐患。更多 控制您还可以使用expiration
和expires
会话值。# Expiration date in epoch seconds from now (persists between requests) $c->session(expiration => 604800); # Expiration date as absolute epoch time (only valid for one request) $c->session(expires => time + 604800); # Delete whole session by setting an expiration date in the past $c->session(expires => 1);
我写了一个小测试脚本以确保它有效:
#!/usr/bin/env perl
use Mojolicious::Lite;
use Time::Local 'timegm';
# set some session variable
get '/test' => sub {
my $self = shift;
$self->session(
expires => timegm(0, 0, 0, 4, 4, 142), # star wars day '42
foo => 42,
);
$self->render_text('foo is set');
};
use Test::More;
use Test::Mojo;
use Mojo::Cookie::Response;
my $t = Test::Mojo->new;
$t->get_ok('/test')->status_is(200)->content_is('foo is set');
my $cookies = Mojo::Cookie::Response->parse($t->tx->res->headers->set_cookie);
is $cookies->[0]->expires, 'Sun, 04 May 2042 00:00:00 GMT', 'right expire time';
done_testing;
输出:
ok 1 - get /test
ok 2 - 200 OK
ok 3 - exact match for content
ok 4 - right expire time
1..4