phildeman, I'm relatively new to sessions, but have recently got some scripts running, first with CGI::Sessions and then with the amazing CGI::Application and one of it's many plugins: CGI::Application::Plugin::Session. Here's a snippet of both approaches:

SET SESSION:

use CGI qw/:cgi/; use Data::Dumper; use CGI::Session; my $query = new CGI; my $session = new CGI::Session("driver:File", $query, {Directory=>'/tm +p'}); $session->param('user_id',$query->param('user_id')); $session->param('user_name',$query->param('user_name')); $session->param('logged-in',1); $session->expires("+15m"); my $cookie = $query->cookie(CGISESSID => $session->id ); print $query->header(-cookie => $cookie);

GET SESSION:

my $session_id = cookie('CGISESSID'); my $session = new CGI::Session("driver:File", $session_id, {Directory= +>'/tmp'}); if ( !$session->param('logged-in') ) { print "Your session expired."; exit(0); } else { my $user_id = $session->param('user_id'); my $user_name = $session->param('user_name'); }

WITH CGI::APPLICATION::PLUGIN::SESSION

use base 'CGI::Application'; use CGI::Application::Plugin::Session; sub cgiapp_init { my $self = shift; $self->session_config( DEFAULT_EXPIRY => '+15m'); } sub cgiapp_prerun { my $self = shift; if ($self->session->param('logged_in')) { $user_id = $self->session->param('user_id'); $user_name = $self->session->param('user_name'); } else { $self->prerun_mode('login'); } } sub setup { my $self = shift; $self->mode_param('rm'); $self->run_modes( 'login' => 'log_in' ); } sub set_session { my $self = shift; $self->session->param(user_name => $user_name); $self->session->param(user_id => $user_id); $self->session->param(logged_in => 1); }

Caveat: this exact code not tested


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to Re: Creating Sessions by bradcathey
in thread Creating Sessions by phildeman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.