1Mojolicious::Sessions(3U)ser Contributed Perl DocumentatiMoonjolicious::Sessions(3)
2
3
4
6 Mojolicious::Sessions - Session manager based on signed cookies
7
9 use Mojolicious::Sessions;
10
11 my $sessions = Mojolicious::Sessions->new;
12 $sessions->cookie_name('myapp');
13 $sessions->default_expiration(86400);
14
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-SHA1 signature.
20
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 {...});
64
65 A callback used to deserialize sessions, defaults to "j" in Mojo::JSON.
66
67 $sessions->deserialize(sub {
68 my $bytes = shift;
69 return {};
70 });
71
72 samesite
73 my $samesite = $sessions->samesite;
74 $sessions = $sessions->samesite('Strict');
75
76 Set the SameSite value on all session cookies, defaults to "Lax". Note
77 that this attribute is EXPERIMENTAL because even though most commonly
78 used browsers support the feature, there is no specification yet
79 besides this draft <https://tools.ietf.org/html/draft-west-first-party-
80 cookies-07>.
81
82 # Disable SameSite feature
83 $sessions->samesite(undef);
84
85 secure
86 my $bool = $sessions->secure;
87 $sessions = $sessions->secure($bool);
88
89 Set the secure flag on all session cookies, so that browsers send them
90 only over HTTPS connections.
91
92 serialize
93 my $cb = $sessions->serialize;
94 $sessions = $sessions->serialize(sub {...});
95
96 A callback used to serialize sessions, defaults to "encode_json" in
97 Mojo::JSON.
98
99 $sessions->serialize(sub {
100 my $hash = shift;
101 return '';
102 });
103
105 Mojolicious::Sessions inherits all methods from Mojo::Base and
106 implements the following new ones.
107
108 load
109 $sessions->load(Mojolicious::Controller->new);
110
111 Load session data from signed cookie.
112
113 store
114 $sessions->store(Mojolicious::Controller->new);
115
116 Store session data in signed cookie.
117
119 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
120
121
122
123perl v5.32.0 2020-07-28 Mojolicious::Sessions(3)