Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Creating Sessions

by bradcathey (Prior)
on Dec 22, 2005 at 01:12 UTC ( [id://518456]=note: print w/replies, xml ) Need Help??


in reply to Creating Sessions

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://518456]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-16 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found