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
| [reply] |
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.
| [reply] |
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-
| [reply] |
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
| [reply] [d/l] [select] |
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.
| [reply] |