| Category: | Web Stuff |
| Author/Contact Info | rob_au |
| Description: | While writing some code based upon the CGI framework concepts discussed here, I put together this small segment of code which introduces very simple session data storage into CGI::Application-inherited classes. This module, very much alpha with respect to code development level, allows sessional data to be stored server-side through the use of Apache::Session::File with _session_id being stored within a client cookie. Use of this module is very simple with the replacement of the use base qw/CGI::Application/; pragma with use base qw/CGI::Application::Session/;. All data to be stored and retrieved should be placed within the $self->{'__SESSION_OBJ'} hash. Additional new method parameters include SESSIONS_EXPIRY, SESSIONS_NAME and SESSIONS_PATH to set the respective parameters of the client-side ID cookie. This code can be expanded and improved upon greatly, but it demonstrates a proof-of-concept for session utilisation within a state-based CGI engine. |
package CGI::Application::Session;
use base qw/ CGI::Application /;
use strict;
use Apache::Session::File;
sub cgiapp_init {
my $self = shift;
my $cgi = $self->query;
my $params;
if (ref($_[0]) eq 'HASH') {
$params = $self->_cap_hash(%{ $_[0] });
} else {
$params = $self->_cap_hash({ @_ });
}
my $id = $cgi->cookie(
-name => ( $params->{'SESSIONS_NAME'} || 'CGI::Application:
+:Session' ),
-path => ( $params->{'SESSIONS_PATH'} || '/' )
);
my %session;
if (defined $id) {
tie %session, 'Apache::Session::File', $id;
} else {
tie %session, 'Apache::Session::File';
$id = $session{'_session_id'};
}
$self->{'__SESSION_OBJ'} = \%session;
$self->{'__SESSION_ID'} = $id;
$self->header_props(
-cookie => $cgi->cookie(
-expires => ( $params->{'SESSIONS_EXPIRY'} || '+1y' ),
-name => ( $params->{'SESSIONS_NAME'} || 'CGI::Applica
+tion::Session' ),
-path => ( $params->{'SESSIONS_PATH'} || '/' ),
-value => $id
)
);
return $self;
}
1;
__END__
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI::Application::Session - A stateful extension to CGI::Application
by Kanji (Parson) on Jan 29, 2002 at 08:23 UTC | |
by rob_au (Abbot) on Jan 29, 2002 at 08:41 UTC |