1Mojolicious::Sessions(3U)ser Contributed Perl DocumentatiMoonjolicious::Sessions(3)
2
3
4

NAME

6       Mojolicious::Sessions - Session manager based on signed cookies
7

SYNOPSIS

9         use Mojolicious::Sessions;
10
11         my $sessions = Mojolicious::Sessions->new;
12         $sessions->cookie_name('myapp');
13         $sessions->default_expiration(86400);
14

DESCRIPTION

16       Mojolicious::Sessions manages sessions based on signed cookies for
17       Mojolicious. All data gets serialized with Mojo::JSON and stored Base64
18       encoded on the client-side, but is protected from unwanted changes with
19       a HMAC-SHA256 signature.
20

ATTRIBUTES

22       Mojolicious::Sessions implements the following attributes.
23
24   cookie_domain
25         my $domain = $sessions->cookie_domain;
26         $sessions  = $sessions->cookie_domain('.example.com');
27
28       Domain for session cookies, not defined by default.
29
30   cookie_name
31         my $name  = $sessions->cookie_name;
32         $sessions = $sessions->cookie_name('session');
33
34       Name for session cookies, defaults to "mojolicious".
35
36   cookie_path
37         my $path  = $sessions->cookie_path;
38         $sessions = $sessions->cookie_path('/foo');
39
40       Path for session cookies, defaults to "/".
41
42   default_expiration
43         my $time  = $sessions->default_expiration;
44         $sessions = $sessions->default_expiration(3600);
45
46       Default time for sessions to expire in seconds from now, defaults to
47       3600. The expiration timeout gets refreshed for every request. Setting
48       the value to 0 will allow sessions to persist until the browser window
49       is closed, this can have security implications though. For more control
50       you can also use the "expiration" and "expires" session values.
51
52         # Expiration date in seconds from now (persists between requests)
53         $c->session(expiration => 604800);
54
55         # Expiration date as absolute epoch time (only valid for one request)
56         $c->session(expires => time + 604800);
57
58         # Delete whole session by setting an expiration date in the past
59         $c->session(expires => 1);
60
61   deserialize
62         my $cb    = $sessions->deserialize;
63         $sessions = $sessions->deserialize(sub ($bytes) {...});
64
65       A callback used to deserialize sessions, defaults to "j" in Mojo::JSON.
66
67         $sessions->deserialize(sub ($bytes) { return {} });
68
69   samesite
70         my $samesite = $sessions->samesite;
71         $sessions    = $sessions->samesite('Strict');
72
73       Set the SameSite value on all session cookies, defaults to "Lax". Note
74       that this attribute is EXPERIMENTAL because even though most commonly
75       used browsers support the feature, there is no specification yet
76       besides this draft <https://tools.ietf.org/html/draft-west-first-party-
77       cookies-07>.
78
79         # Disable SameSite feature
80         $sessions->samesite(undef);
81
82   secure
83         my $bool  = $sessions->secure;
84         $sessions = $sessions->secure($bool);
85
86       Set the secure flag on all session cookies, so that browsers send them
87       only over HTTPS connections.
88
89   serialize
90         my $cb    = $sessions->serialize;
91         $sessions = $sessions->serialize(sub ($hash) {...});
92
93       A callback used to serialize sessions, defaults to "encode_json" in
94       Mojo::JSON.
95
96         $sessions->serialize(sub ($hash) { return '' });
97

METHODS

99       Mojolicious::Sessions inherits all methods from Mojo::Base and
100       implements the following new ones.
101
102   load
103         $sessions->load(Mojolicious::Controller->new);
104
105       Load session data from signed cookie.
106
107   store
108         $sessions->store(Mojolicious::Controller->new);
109
110       Store session data in signed cookie.
111

SEE ALSO

113       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
114
115
116
117perl v5.36.0                      2022-07-22          Mojolicious::Sessions(3)
Impressum