Mojolic...Sessions(3)
π Mojolic...Sessions β session manager based on signed cookies πͺ
So this is Mojolic...Sessions(3), bestie β it lives in the User Contributed Perl Documentation corner of your man pages, and it's literally the session manager for Mojolicious. Let's get into it β¨
πΈ SYNOPSIS πΈ
use Mojolicious::Sessions;
my $sessions = Mojolicious::Sessions->new;
$sessions->cookie_name('myapp');
$sessions->default_expiration(86400);
That's the whole vibe check right there. New it up, name your cookie, pick how long it lives. Slay π
π¦ DESCRIPTION π¦
Okay so Mojolicious::Sessions manages sessions based on signed cookies for Mojolicious. All data gets serialized with Mojo::JSON and stored Base64 encoded on the client-side β yep, on the client, main character energy β but it's protected from unwanted changes with a HMAC-SHA256 signature. So like, people can peek, but not mess with it. π₯
π ATTRIBUTES π
Mojolicious::Sessions implements the following attributes, bestie β every single one, no skips π«Ά
π cookie_domain
my $domain = $sessions->cookie_domain;
$sessions = $sessions->cookie_domain('.example.com');
Domain for session cookies. Not defined by default β it's giving "figure it out yourself" π
π cookie_name
my $name = $sessions->cookie_name;
$sessions = $sessions->cookie_name('session');
Name for session cookies, and it defaults to "mojolicious". Iconic of them tbh.
π cookie_path
my $path = $sessions->cookie_path;
$sessions = $sessions->cookie_path('/foo');
Path for session cookies, defaults to "/". Classic β¨
π default_expiration
my $time = $sessions->default_expiration;
$sessions = $sessions->default_expiration(3600);
Default time for sessions to expire, in seconds from now, and it defaults to 3600. The expiration timeout gets refreshed for every request β like a little refresh cycle, we love to see it π
β οΈ Setting the value to 0 will allow sessions to persist until the browser window is closed. That can have security implications though β ngl, be careful with that one π
For more control you can also use the "expiration" and "expires" session values:
# Expiration date in 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);
That last one literally sending the session into the past to delete it? Time travel as a delete button, we stan π
π deserialize
my $cb = $sessions->deserialize;
$sessions = $sessions->deserialize(sub ($bytes) {...});
A callback used to deserialize sessions, defaults to "j" in Mojo::JSON.
$sessions->deserialize(sub ($bytes) { return {} });
π encrypted
my $bool = $sessions->encrypted;
$sessions = $sessions->encrypted($bool);
Use encrypted session cookies instead of merely cryptographically signed ones. Upgrade era πβ¨
π samesite
my $samesite = $sessions->samesite;
$sessions = $sessions->samesite('Strict');
Set the SameSite value on all session cookies, defaults to "Lax".
# Disable SameSite feature
$sessions->samesite(undef);
π secure
my $bool = $sessions->secure;
$sessions = $sessions->secure($bool);
Set the secure flag on all session cookies, so that browsers send them only over HTTPS connections. HTTPS or nothing, periodt π
π serialize
my $cb = $sessions->serialize;
$sessions = $sessions->serialize(sub ($hash) {...});
A callback used to serialize sessions, defaults to "encode_json" in Mojo::JSON.
$sessions->serialize(sub ($hash) { return '' });
π§ METHODS π§
Mojolicious::Sessions inherits all methods from Mojo::Base and implements the following new ones. Just two, but they're doing the most π
β load
$sessions->load(Mojolicious::Controller->new);
Load session data from signed cookie. Load it up, bestie π«Ά
β store
$sessions->store(Mojolicious::Controller->new);
Store session data in signed cookie. Put it back, sealed and signed π₯
π SEE ALSO π
MojoliciousMojolicious::Guides- <https://mojolicious.org>
Okay that's the whole page, no cap π β brought to you by perl v5.42.0, dated 2026-07-15, from the Mojolic...Sessions(3) man page. Go forth and session responsibly, bestie πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms