Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Creating Sessions

by phildeman (Scribe)
on Dec 21, 2005 at 20:13 UTC ( [id://518423]=perlquestion: print w/replies, xml ) Need Help??

phildeman has asked for the wisdom of the Perl Monks concerning the following question:

I am new to using sessions. I understand that you can use sessions to store form variables, especially useful when there are multiple forms and users need to be able to toggle back and forth between forms before their final submission.

Can any direct me to books, point me to other resources, or give me a quick tutorial on how to set-up/create sessions?

Thanks.

Replies are listed 'Best First'.
Re: Creating Sessions
by ptum (Priest) on Dec 21, 2005 at 20:31 UTC

    As referenced in this recent node: Create CGI login for webpage, you might take a look at CGI::Session::Tutorial.

    Update: (Copied from duplicate node) For more simple problems, sometimes it is enough to pass previous form information forward using hidden tags.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Creating Sessions
by davido (Cardinal) on Dec 21, 2005 at 21:56 UTC

    There's the POD for CGI::Session, and CGI::Session::Tutorial (as already mentioned). I wanted to add that there is a decent discussion on session management in the Mouse book: CGI Programming with Perl (2nd Edition), published by O'Reilly & Associates. You may find it at the library if you don't want to own your own copy. I found the book useful. The second edition has more on sessions, and more on security than the first edition, so unless you're more interested in the nuts and bolts of the common gateway interface itself, stick with the 2nd edition.


    Dave

      Thanks Dave, I am currently referring to CGI Programming with Perl (2nd Edition). It seems to have a wealth of information regarding sessions and maintaining state.

      -Phil-
Re: Creating Sessions
by bradcathey (Prior) on Dec 22, 2005 at 01:12 UTC

    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
Re: Creating Sessions
by kutsu (Priest) on Dec 21, 2005 at 20:59 UTC

    There's a wealth of information on PM about creating sessions so your first stop here should be Super Search :). However, while searching for sessions and the various aspects that go along with sessions (like cookies), you might also look for web or cgi frameworks. Though they might take a while to learn and get used to they can save a ton of work in the long run (merlyn wrote a great article about these). My favorites web frameworks avaliable are CGI::Application and catalyst btw.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found