[PerlChina] the plugin CGI::Application::Plugin::Session issue

Qiang shijialee at gmail.com
Tue Jun 10 10:30:47 PDT 2008


Mike.G wrote:
> Hi, list
> I got one issue when I use the plugin CGI::Application::Plugin:
> :Session
> 
> I have a parent package(Class), named "FWebApp",
> and this class extends the CGI::Application.
> I did some setting work in the method cgiapp_init
> 
> like this:
> 
> sub cgiapp_init {
>     my $this = shift;
> 
>     #get the database configuration
>     my $datasource = $this->cfg('data_source');
>     my $data_user = $this->cfg('data_username');
>     my $data_auth = $this->cfg('data_userauth');
>     my $attr = $this->cfg('data_attr');
>     
>     #configuration the Database
>     $this->dbh_config($datasource,
>         $data_user,
>         $data_auth, 
>         $attr);
>     
> 
>     #configuration the Session
>     $this->session_config(
>         CGI_SESSION_OPTIONS => [
>         "driver:mysql",
>         $this->query,
>         {
>             Handle=>$this->dbh
>         }],
> 
>         COOKIE_PARAMS => {
>         -domain => $this->cfg('site_domain'),
>         -expires=>'+40m',
>         -path=>'/',
>         -secure=>1,
>         },
> 
>     );
> 
>     #set the template path
>     $this->tmpl_path($this->cfg('template_path'));
> 
> 
> 
> and the session configuration look like is ok.
> 
> and so, just now, i assume I have a action: it is register .
> so, I will write a new module named:
> 
> package FWebApp::Register;
> =head1  NAME
> 
> FWebApp::Register - show the register page
> 
> =head1  SYNOPSIS
> 
> extends the Class FWebApp,
> show the register page
> 
> =cut
> use strict;
> use warnings;
> use base qw/FWebApp/;
> use CGI::Application::Plugin::AutoRunmode;
> use CGI::Application::Plugin::DBH qw/dbh_config dbh/;
> use CGI::Application::Plugin::ConfigAuto qw/cfg/;
> use CGI::Application::Plugin::Session;
> 
> 
> # the default requirement mode
> # show the register page.
> sub show_register: StartRunmode {
>     my $this = shift;
> 
>     # if that user had login, we will jump to home page
>     my $template = undef;
>     if ( $this->session->param('loginname') ) {
>         $template = $this->load_tmpl('had_login.html');
>         $template->param('HOMEPAGE', 'index.html');
>     } else {
>         $template = $this->load_tmpl('register.html');
>     }
> 
>         
>     return $template->output;
> 
> }
> 
> 
> 
> 
> 1;
> 
> 
> it is extends FWebApp, but when I had prepared a script:
> like this:
> #!/usr/local/bin/perl -w -T
> use strict;
> use lib qw(. ../lib);
> use CGI::Application::Plugin::ConfigAuto;
> use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
> use FWebApp::Register;
> 
> my $app = FWebApp::Register->new(
>     PARAMS => {
>         cfg_file => 'config.pl' } );
> 
> $app->run;
> 
> 
> _*when I refresh that script, we always got the difference session id, 
> why?*_
> 
> 

我用 CGI::Application 但并不怎么使用 CGI::Application::Plugin::Session 
(缩为 CAP::Session)。 大致看了你的例子, script 每次运行都会调用 sub 
show_register 里的 $this->session->param('loginname') 来检查用户是否已登 
录,如果没有登录,$this->session 的调用就会生成一个新的 CGI::Session 
object 因此生产一个新 session id. 你的 script 没有登录的那部分,即每次都 
是一个非登录的用户访问,自然每次都拿到一个新 session id 了。

另外,CGI::Application 里一般都在 base class 里把要用到的 CAP 的 plugins 
load 进去,例如 CAP::DBH, CAP::ConfigAuto 等,没有必要在子包里在 use 了。

> thanks
> 
> 
> Mike.G

HTH,

Qiang


More information about the China-pm mailing list