Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

CGI::Application::Session - A stateful extension to CGI::Application

by rob_au (Abbot)
on Jan 28, 2002 at 17:34 UTC ( [id://142050]=sourcecode: print w/replies, xml ) Need Help??
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

    I'm sure the issue of namespace will be won out by whomever submits to CPAN first ;), but there's already a module of the same name included in the cgi-app-login project (an extension for cookie auths).

    Update: 2002/03/01 - looks like the cgi-app-login project has gone AWOL ... URL returns a 404. :/

        --k.


      Thanks for the link - As it is, I'm not overly interested in uploading this code to CPAN given its very alpha state. One thing though that did make me shudder looking at that other code was the hand-rolled flat-text file session storage with no locking or concurrency considerations - I was aware of some of the issues involved in such an engine and opted for Apache::Session and the multiple backends that it offers instead (effectively side-stepping the issue to a great extent within my own code).

      As it is, I am working on integrating this session control into a more flexible framework that allows for pre- and post- processing of control in addition to some of the features already offered by other session and state CGI frameworks.

       

      perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://142050]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found