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

Hello, Monks. I use a CGI::Application and want to use a session. I know about session plugin, but I have some trouble. When I get $self->session a plugin creates a new session, but I do not need any sessions until user logged in. For example, I do this to check user
unless ($self->session->param('user_id')) { $self->prerun_mode('login'); }
When a user simply looks at FAQ, or login page, or help page the cgiapp creates a session, but I do not need it. Usually I do something like this in other projects:
my $s = CGI::Session->load("driver:mysql", undef, { DataSource => "dbi:mysql:$DB_Name", User => $DB_User, Password => $DB_Pass }); $app->redirect() if ($s->is_empty);
In this case I create session only after login
$s = $s->new; $s->param('user_id', $user->id);
Is it possible to work with CGI::Application::Plugin::Session such as CGI::Session method load()?

Replies are listed 'Best First'.
Re: Session->load and CGI::Application
by Herkum (Parson) on May 22, 2008 at 22:58 UTC

    CGI::Application::Plugin::Session uses 'lazy loading'. In other words it will not create a session until you actually need it. The advantage of this is you don't have to 'load' a session object, it will do this for you.

      as I wrote above, cgiapp plugin "creates" the session always. For example, if user simply view start page of site, the plugin will create a session object and save it in DB. If user not login and simply browse site the plugin will create sessions objects. But I want to create session only when user has logged in. I do not need a session object when user simply browse site.