无论我给expires()
orexpires_delta()
的过期值是什么,cookie 的过期时间总是一小时。如何更改它以使会话和 cookie 过期时间匹配?
问问题
542 次
1 回答
3
尽管我喜欢 vti 的工作,但该发行版看起来已经过时并且在过去已被替换。今天,在Mojolicious::Sessions中解释了设置会话过期日期的标准方法:
default_expiration
my $time = $sessions->default_expiration; $sessions = $sessions->default_expiration(3600);
会话从现在开始以秒为单位过期的默认时间,默认为
3600
. 每次请求都会刷新过期超时。将该值设置为 0 将允许会话持续到浏览器窗口关闭,但这可能会产生安全隐患。要获得更多控制,您还可以使用expiration
和expires
session 值。# 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
于 2012-12-14T00:02:07.693 回答