A simple wrapper around apache::session
my $wrapper = Apache::Session::Wrapper->new( class => 'MySQL', handle => $dbh, cookie_name => 'example-dot-com-cookie', ); # will get an existing session from a cookie, or create a new session # and cookie if needed $wrapper->session->{foo} = 1;
This module is a simple wrapper around Apache::Session which provides some methods to simplify getting and setting the session id.
It can uses cookies to store the session id, or it can look in a provided object for a specific parameter. Alternately, you can simply provide the session id yourself in the call to the \*(C`session()\*(C' method.
If you're using Mason, you should probably take a look at \*(C`MasonX::Request::WithApacheSession\*(C' first, which integrates this module directly into Mason.
This class provides the following public methods:
new This method creates a new \*(C`Apache::Session::Wrapper\*(C' object. If the parameters you provide are not correct (wrong type, missing parameters, etc.), this method throws an \*(C`Apache::Session::Wrapper::Exception::Params\*(C' exception. You can treat this exception as a string if you want.
session This method returns a hash tied to the \*(C`Apache::Session\*(C' class. This method accepts an optional \*(L"session_id\*(R" parameter.
delete_session This method deletes the existing session from persistent storage. If you are using the built-in cookie handling, it also deletes the cookie in the browser.
This module accepts quite a number of parameters, most of which are simply passed through to \*(C`Apache::Session\*(C'. For this reason, you are advised to familiarize yourself with the \*(C`Apache::Session\*(C' documentation before attempting to configure this module.
You can also register \*(C`Apache::Session\*(C' classes, or the classes used for doing the work in \*(C`Apache::Session::Flex\*(C'. See \s-1REGISTERING\s0 \s-1CLASSES\s0 for details.
The following classes are already supported and do not require registration:
Apache::Session::MySQL
Apache::Session::Postgres
Apache::Session::Oracle
Apache::Session::Informix
Apache::Session::Sybase
Apache::Session::File
Apache::Session::DB_File
Apache::Session::PHP
Apache::Session::Flex
The following classes can be used with \*(C`Apache::Session::Flex\*(C':
Apache::Session::Store::MySQL
Apache::Session::Store::Postgres
Apache::Session::Store::Informix
Apache::Session::Store::Oracle
Apache::Session::Store::Sybase
Apache::Session::Store::File
Apache::Session::Store::DB_File
Apache::Session::Store::PHP
Apache::Session::Lock::MySQL
Apache::Session::Lock::File
Apache::Session::Lock::Null
Apache::Session::Lock::Semaphore
Apache::Session::Generate::MD5
Apache::Session::Generate::ModUsertrack
Apache::Session::Serialize::Storable
Apache::Session::Serialize::Base64
Apache::Session::Serialize::Sybase
Apache::Session::Serialize::UUEncode
Apache::Session::Serialize::PHP
class => class name The name of the \*(C`Apache::Session\*(C' subclass you would like to use. This module will load this class for you if necessary. This parameter is required.
always_write => boolean If this is true, then this module will ensure that \*(C`Apache::Session\*(C' writes the session. If it is false, the default \*(C`Apache::Session\*(C' behavior is used instead. This defaults to true.
allow_invalid_id => boolean If this is true, an attempt to create a session with a session id that does not exist in the session storage will be ignored, and a new session will be created instead. If it is false, a \*(C`Apache::Session::Wrapper::Exception::NonExistentSessionID\*(C' exception will be thrown instead. This defaults to true.
session_id => string Try this session id first and use it if it exist. If the session does not exist, it will ignore this parameter and make a new session.
use_cookie => boolean If true, then this module will use one of \*(C`Apache::Cookie\*(C', \*(C`Apache2::Cookie\*(C' or \*(C`CGI::Cookie\*(C' (as appropriate) to set and read cookies that contain the session id.
cookie_name => name This is the name of the cookie that this module will set. This defaults to \*(L"Apache-Session-Wrapper-cookie\*(R". Corresponds to the \*(C`Apache::Cookie\*(C' \*(L"-name\*(R" constructor parameter.
cookie_expires => expiration How long before the cookie expires. This defaults to 1 day, \*(L"+1d\*(R". Corresponds to the \*(L"-expires\*(R" parameter. As a special case, you can set this value to \*(L"session\*(R" to have the \*(L"-expires\*(R" parameter set to undef, which gives you a cookie that expires at the end of the session.
cookie_domain => domain This corresponds to the \*(L"-domain\*(R" parameter. If not given this will not be set as part of the cookie. If it is undefined, then no \*(L"-domain\*(R" parameter will be given.
cookie_path => path Corresponds to the \*(L"-path\*(R" parameter. It defaults to \*(L"/\*(R".
cookie_secure => boolean Corresponds to the \*(L"-secure\*(R" parameter. It defaults to false.
cookie_resend => boolean By default, this parameter is true, and the cookie will be sent for every request. If it is false, then the cookie will only be sent when the session is created. This is important as resending the cookie has the effect of updating the expiration time.
header_object => object When running outside of mod_perl, you must provide an object to which the cookie header can be added. This object must provide an \*(C`err_headers_out()\*(C' or \*(C`headers_out()\*(C' method. Under mod_perl 1, this will default to the object returned by \*(C`Apache->request()\*(C'. Under mod_perl 2 we call \*(C`Apache2::RequestUtil->request()\*(C'
param_name => name If set, then this module will first look for the session id in the object specified via \*(L"param_object\*(R". This parameter determines the name of the parameter that is checked. If you are also using cookies, then the module checks the param object first, and then it checks for a cookie.
param_object => object This should be an object that provides a \*(C`param()\*(C' method. This object will be checked to see if it contains the parameter named in \*(L"params_name\*(R". This object will probably be a \*(C`CGI.pm\*(C' or \*(C`Apache::Request\*(C' object, but it doesn't have to be.
These parameters are simply passed through to \*(C`Apache::Session\*(C'.
data_source => \s-1DSN\s0 Corresponds to the \*(C`DataSource\*(C' parameter passed to the DBI-related session modules.
user_name => user name Corresponds to the \*(C`UserName\*(C' parameter passed to the DBI-related session modules.
password => password Corresponds to the \*(C`Password\*(C' parameter passed to the DBI-related session modules. Defaults to undef.
handle => \s-1DBI\s0 handle Corresponds to the \*(C`Handle\*(C' parameter passed to the DBI-related session modules. This cannot be set via the httpd.conf file, because it needs to be an actual Perl variable, not the name of that variable.
table_name => table name Corresponds to the \*(C`TableName\*(C' paramaeter passed to DBI-related modules.
lock_data_source => \s-1DSN\s0 Corresponds to the \*(C`LockDataSource\*(C' parameter passed to \*(C`Apache::Session::MySQL\*(C'.
lock_user_name => user name Corresponds to the \*(C`LockUserName\*(C' parameter passed to \*(C`Apache::Session::MySQL\*(C'.
lock_password => password Corresponds to the \*(C`LockPassword\*(C' parameter passed to \*(C`Apache::Session::MySQL\*(C'. Defaults to undef.
lock_handle => \s-1DBI\s0 handle Corresponds to the \*(C`LockHandle\*(C' parameter passed to the DBI-related session modules. As with the \*(C`handle\*(C' parameter, this cannot be set via the httpd.conf file.
commit => boolean Corresponds to the \*(C`Commit\*(C' parameter passed to the DBI-related session modules.
transaction => boolean Corresponds to the \*(C`Transaction\*(C' parameter.
directory => directory Corresponds to the \*(C`Directory\*(C' parameter passed to \*(C`Apache::Session::File\*(C'.
lock_directory => directory Corresponds to the \*(C`LockDirectory\*(C' parameter passed to \*(C`Apache::Session::File\*(C'.
file_name => file name Corresponds to the \*(C`FileName\*(C' parameter passed to \*(C`Apache::Session::DB_File\*(C'.
store => class Corresponds to the \*(C`Store\*(C' parameter passed to \*(C`Apache::Session::Flex\*(C'.
lock => class Corresponds to the \*(C`Lock\*(C' parameter passed to \*(C`Apache::Session::Flex\*(C'.
generate => class Corresponds to the \*(C`Generate\*(C' parameter passed to \*(C`Apache::Session::Flex\*(C'.
serialize => class Corresponds to the \*(C`Serialize\*(C' parameter passed to \*(C`Apache::Session::Flex\*(C'.
textsize => size Corresponds to the \*(C`textsize\*(C' parameter passed to \*(C`Apache::Session::Sybase\*(C'.
long_read_len => size Corresponds to the \*(C`LongReadLen\*(C' parameter passed to \*(C`Apache::Session::MySQL\*(C'.
n_sems => number Corresponds to the \*(C`NSems\*(C' parameter passed to \*(C`Apache::Session::Lock::Semaphore\*(C'.
semaphore_key => key Corresponds to the \*(C`SemaphoreKey\*(C' parameter passed to \*(C`Apache::Session::Lock::Semaphore\*(C'.
mod_usertrack_cookie_name => name Corresponds to the \*(C`ModUsertrackCookieName\*(C' parameter passed to \*(C`Apache::Session::Generate::ModUsertrack\*(C'.
save_path => path Corresponds to the \*(C`SavePath\*(C' parameter passed to \*(C`Apache::Session::PHP\*(C'.
When run under mod_perl, this module attempts to first use \*(C`Apache::Cookie\*(C' for cookie-handling. Otherwise it uses \*(C`CGI::Cookie\*(C' as a fallback.
If it ends up using \*(C`CGI::Cookie\*(C' then you must provide a \*(L"header_object\*(R" parameter. This object must have an \*(C`err_headers_out()\*(C' or \*(C`headers_out()\*(C' method. It looks for these methods in that order. The method is expected to return an object with an \s-1API\s0 like \*(C`Apache::Table\*(C'. It calls \*(C`add()\*(C' on the returned method to add a \*(L"Set-Cookie\*(R" header.
In order to support any \*(C`Apache::Session\*(C' subclasses, this module provides a simple registration mechanism.
You can register an \*(C`Apache::Session\*(C' subclass, or a class intended to provide a class that implements something required by \*(C`Apache::Session::Flex\*(C'.
This is done by calling \*(C`Apache::Session::Wrapper->RegisterClass()\*(C':
Apache::Session::Wrapper->RegisterClass ( name => 'MyClass', required => [ [ qw( param1 param2 ) ], [ qw( param3 param4 ) ] ], optional => [ 'optional_p' ], );
Apache::Session::Wrapper->RegisterClass ( name => 'Apache::Session::MyFile', required => 'File', optional => 'File', );
The \*(C`RegisterClass()\*(C' method takes the following options:
name This should be the name of the class you are registering. The actual class must start with \*(L"Apache::Session::\*(R", but this part does not need to be included when registering the class (it's optional).
required These are the required parameters for this class. The value of this parameter can either be a string or a reference to an array of array references. If it is a string, then it identifies an existing \*(C`Apache::Session\*(C' subclass which is already registered or built-in, like \*(L"File\*(R" or \*(L"Postgres\*(R". If it an array reference, then that reference should in turn contain one or more array references. Each of those contained references represents one set of required parameters. When an \*(C`Apache::Session::Wrapper\*(C' object is constructed, only one of these sets must be passed in. For example: required => [ [ qw( p1 p2 ) ], [ qw( p2 p3 p4 ) ] ] This says that either \*(L"p1\*(R" and \*(L"p2\*(R" must be provided, or \*(L"p2\*(R", \*(L"p3\*(R", and \*(L"p4\*(R". If there are no required parameters for this class, then the \*(L"required\*(R" parameter can be omitted.
optional This specifies optional parameters, and should just be a simple array reference.
Registering a subclass that can be used with \*(C`Apache::Session::Flex\*(C' is very similar to registering a complete class:
Apache::Session::Wrapper->RegisterFlexClass ( name => 'MyClass', type => 'Store', required => [ [ qw( param1 param2 ) ], [ qw( param3 param4 ) ] ], optional => [ 'optional_p' ], );
Apache::Session::Wrapper->RegisterFlexClass ( name => 'Apache::Session::Store::MyFile', type => 'store', required => 'File', optional => 'File', );
The \*(C`RegisterFlexClass()\*(C' method has the same parameters as \*(C`RegisterClass()\*(C', but it also requires a \*(L"type\*(R" parameter. This must be one of \*(L"store\*(R", \*(L"lock\*(R", \*(L"generate\*(R", or \*(L"serialize\*(R".
This class provides a simple hook for subclasses. Before trying to get a session id from the \s-1URL\s0 or cookie, it calls a method named \*(C`_get_session_id()\*(C'. In this class, that method is a no-op, but you can override this in a subclass.
This class is a \*(C`Class::Container\*(C' subclass, so if you accept additional constructor parameters, you should declare them via the \*(C`valid_params()\*(C' method.
As can be seen by the number of parameters above, \*(C`Apache::Session\*(C' has way too many possibilities for me to test all of them. This means there are almost certainly bugs.
Please submit bugs to the \s-1CPAN\s0 \s-1RT\s0 system at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Apache%3A%3ASession%3A%3AWrapper or via email at [email protected].
Support questions can be sent to me at my email address, shown below.
Dave Rolsky, <[email protected]>
Copyright (c) 2003-2006 David Rolsky. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the \s-1LICENSE\s0 file included with this module.