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 🫢

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" πŸ™ˆ

my $name  = $sessions->cookie_name;
$sessions = $sessions->cookie_name('session');

Name for session cookies, and it defaults to "mojolicious". Iconic of them tbh.

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 🌈


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