Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Creating Sessions

by phildeman (Sexton)
on Dec 21, 2005 at 20:13 UTC ( #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?


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 (Archbishop) 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.


      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.

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:


    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);


    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'); }


    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

    "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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://518423]
Approved by mattriff
[shmem]: well finally.
[stevieb]: nice! I just finished a GPS "take me home" device last week, and did a bunch of software updates to it yesterday. I also created a pseudo chip with an Arduino to simulate an IC, where it responds to register read/writes over the I2C bus...
[stevieb]: ...from an I2C master. It's ugly and there are many changes I'm going to make, but I had not done anything like it before. It's designed for my RPi:: automated test platform; a system that does CI on *all* my RPi modules.
[shmem]: pseudo chip?
[stevieb]: well, what happens is the Arduino 'listens' for requests r/w, and does the appropriate thing when it's interrupted based on the 'register' address sent in. It's ugly as it was my first attempt, but I've got great new ideas I'm just sitting.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-06-25 22:40 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.