MasonX::Request::WithApacheSession - Add a session to the Mason Request object


In your httpd.conf file:

  PerlSetVar  MasonRequestClass            MasonX::Request::WithApacheSession
  PerlSetVar  MasonSessionCookieDomain
  PerlSetVar  MasonSessionClass            Apache::Session::File
  PerlSetVar  MasonSessionDirectory        /tmp/sessions/data
  PerlSetVar  MasonSessionLockDirectory    /tmp/sessions/locks

Or when creating an ApacheHandler object:

  my $ah =
          ( request_class => 'MasonX::Request::WithApacheSession',
            session_cookie_domain  => '',
            session_class          => 'Apache::Session::File',
            session_directory      => '/tmp/sessions/data',
            session_lock_directory => '/tmp/sessions/locks',

In a component:

  $m->session->{foo} = 1;
  if ( $m->session->{bar}{baz} > 1 ) { ... }


This module integrates Apache::Session into Mason by adding methods to the Mason Request object available in all Mason components.

Any subrequests created by a request share the same session.


To use this module you need to tell Mason to use this class for requests. This can be done in one of two ways. If you are configuring Mason via your httpd.conf file, simply add this:

  PerlSetVar  MasonRequestClass  MasonX::Request::WithApacheSession

If you are using a file, simply add this parameter to the parameters given to the ApacheHandler constructor:

  request_class => 'MasonX::Request::WithApacheSession'


This class adds two methods to the Request object.

  • session

    This method returns a hash tied to the Apache::Session class.

  • delete_session

    This method deletes the existing session from persistent storage. If you are using the built-in cookie mechanism, it also deletes the cookie in the browser.


This module accepts quite a number of parameters, most of which are simply passed through to Apache::Session::Wrapper. For this reason, you are advised to familiarize yourself with the Apache::Session::Wrapper documentation before attempting to configure this module.

If you are creating your own Interp/ApacheHandler/CGIHandler object in a script or module, you should pass this object the parameters intended for Apache::Session::Wrapper, prefixed with "session_". So to set the "class" parameter for Apache::Session::Wrapper, you pass in a "session_class" parameter.

If you are configuring Mason via your httpd.conf file, you should pass the "StudlyCaps" version of the name, prefixed by "MasonSession". So the "class" parameter would be "MasonSessionClass".

A few examples:

  • class becomes session_class / MasonSessionClass

  • always_write becomes session_always_write / MasonSessionAlwaysWrite

When running under ApacheHandler or CGIHandler, this module takes care of passing the "header_object" and "param_object" parameters to Apache::Session::Wrapper. These will be the Apache::Request or objects, as applicable.

The "cookie_name" parameter defaults to "MasonX-Request-WithApacheSession-cookie" when you use this module, instead of "Apache-Session-Wrapper-cookie".

Finally, for backwards compatiblity, this module accepts a "session_args_param" parameter, which corresponds to the "param_name" parameter for Apache::Session::Wrapper.


As can be seen by the number of parameters above, Apache::Session has way too many possibilities for me to test all of them. This means there are almost certainly bugs.

Bug reports and requests for help should be sent to the mason-users list. See for more details.


Dave Rolsky, <>


Brad Lhotsky, <>