Net::H...ession(3pm)

πŸ“¦
version 1.1.1 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Net::HTTPServer::Session β€” bestie, your server finally remembers people πŸ’–

okay so this is Net::HTTPServer::Session, a little Perl module from the User Contributed Perl Documentation corner of your 3pm man pages, and it's literally here to give your HTTP server side client sessions. yes. server-side memory. for each person. it's giving "the server actually knows who you are" energy ✨


✨ NAME ✨

Net::HTTPServer::Session β€” HTTP server side client session, periodt πŸ’…


🌸 SYNOPSIS 🌸

Net::HTTPServer::Session handles server side client sessions. that's the whole vibe, no notes 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea πŸ“ β€” Net::HTTPServer::Session gives you a server side data store for sessions that belong to specific clients. it does this by dropping a cookie on the browser, and that cookie is how the server knows which session to restore to the user when they come back. iykyk.

this is modelled after the PHP session concept, bestie, so if you've ever touched PHP sessions this will feel like coming home 🏠

and here's the important bit, no cap: the session is valid for 4 hours from the last time the cookie was sent. so it's a rolling window, not a hard timer from when it was born. lowkey generous of it ⏰


πŸŽ€ EXAMPLES πŸŽ€

here's a whole pageHandler sub that shows the entire lifecycle β€” login, logout, remembering your fave person, all of it πŸ‘‘

sub pageHandler {
    my $request = shift;

    my $session = $request->Session();

    my $response = $request->Response();

    # Logout
    $session->Destroy() if $request->Env("logout");

    $response->Print("<html><head><title>Hi there</title></head><body>");

    # If the user specified a username on the URL, then save it.
    if ($request->Env("username"))
    {
        $session->Set("username",$request->Env("username"));
    }

    # If there is a saved username, then use it.
    if ($session->Get("username"))
    {
        $response->Print("Hello, ",$session->Get("username"),"!");
    }
    else
    {
        $response->Print("Hello, stranger!");
    }

    $response->Print("</body></html>");

    return $response;
}

and the above would behave exactly like this, watch the glow-up happen in real time 🌈:

http://server/page                - Hello, stranger!
http://server/page?username=Bob   - Hello, Bob!
http://server/page                - Hello, Bob!
http://server/page?username=Fred  - Hello, Fred!
http://server/page                - Hello, Fred!
http://server/page?logout=1       - Hello, stranger!
http://server/page                - Hello, stranger!

see how it just remembers? main character energy fr fr πŸ˜­πŸ’–


πŸ‘‘ METHODS πŸ‘‘

πŸ’œ Delete(var)

delete the specified variable from the session. bye bestie, we don't know her anymore πŸ™ˆ

πŸ’€ Destroy()

destroy the session. and when we say destroy we mean it β€” the server side data is deleted and the cookie will be expired. full send. it's over. no crumbs left πŸ’…

🧚 Exists(var)

returns whether the specified variable exists in the session. a simple "does this thing live here or not" vibe check βœ…

⭐ Get(var)

return the value of the specified variable from the session if it exists, and undef otherwise. no value? no problem, you just get undef 🫢

🌸 Set(var,value)

store the specified value in the session β€” and it can be a scalar or a reference to any Perl data structure. hashes, arrays, nested chaos, whatever you want, it holds it all πŸ’–


πŸŽ€ AUTHOR πŸŽ€

Ryan Eatmon


Copyright (c) 2003-2005 Ryan Eatmon <reatmon@mail.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


ngl if you're out here storing sessions server-side with a cookie and a four hour rolling window, you're doing amazing sweetie 🫢

so that's the whole page, bestie β€” perl v5.36.0, dated 2022-12-06, straight from the Net::HTTPServer::Session(3pm) man page. go forth and remember your users, and slay πŸ’…βœ¨πŸŽ€