#===========================================================================
Copyright (C) 2008 by Nik Ogura. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Bug reports and comments to nik.ogura@gmail.com.
CGI::Lazy::Session
use CGI::Lazy; our $q = CGI::Lazy->new({ tmplDir => "/templates", jsDir => "/js", plugins => { mod_perl => { PerlHandler => "ModPerl::Registry", saveOnCleanup => 1, }, ajax => 1, dbh => { dbDatasource => "dbi:mysql:somedatabase:localhost", dbUser => "dbuser", dbPasswd => "letmein", dbArgs => {"RaiseError" => 1}, }, session => { sessionTable => 'SessionData', sessionCookie => 'frobnostication', saveOnDestroy => 1, expires => '+15m', }, }, });
CGI::Lazy::Session is for maintaining state between requests. It's enabled in the config file or config hash. Once it's enabled, any calls to $q->header will automatically include a cookie that will be used to retrieve session data.
To function, the session needs the following arguments:
sessionTable => name of table to store data in sessionCookie => name of the cookie expires => how long a session can sit idle before expiring
By default, sessions are automatically saved when the Lazy object is destroyed, or in the cleanup stage of the request cycle for mod_perl apps. Both mechanisms are enabled by default. (call me paranoid) Should you wish to disable the save on destroy: saveOnDestroy => 0
If the key is missing from the config, it's as if it was set to 1. You will have to set it to 0 to disable this functionality. Same goes for the mod_perl save. See CGI::Lazy::ModPerl for details
Session data is stored in the db as JSON formatted text at present. Fancier storage (for binary data and such) will have to wait for subsequent releases.
The session table must have the following fields at a bare minimum:
sessionID not null, primary key, varchar(25) data text (mysql) large storage (clob in oracle)
returns reference to CGI::Lazy::CookieMonster object
returns reference to the config object
returns reference to the CGI::Lazy::Session::Data object
returns reference to CGI::Lazy::DB object
returns 1 if session has been expired, either explicitly or by timeout
Returns time in epoch when session expires.
Called internally on CGI::Lazy::Session::Data creation. Queries db for session data
returns session id
Constructor. Creates new session.
valid session ID string
Opens a previous session, or creates a new one. If it's opening an existing session, it will check to see that the session given has not expired. If it has, it will create a new one.
CGI::Lazy object
valid session ID
Parses expiration string from session plugin and returns time in epoch when session should expire.
Currently only can parse seconds, minutes, hours and days. Is more really necessary?
Epoch returned from time() function
Returns reference to CGI::Lazy object
Returns name of session cookie specified by session plugin
Returns session id
Returns session table name specified by session plugin
Saves session variable to database
Terminates the session. Session data object will have the 'terminated' flag set.
To install CGI::Lazy, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Lazy
CPAN shell
perl -MCPAN -e shell install CGI::Lazy
For more information on module installation, please visit the detailed CPAN module installation guide.