NAME
Prancer::Session
SYNOPSIS
Sessions are just as important in a web application as GET and POST parameters. So if you have configured your application for sessions then every request will include a session object specific to that request.
sub handler {
my ($self, $env, $request, $response, $session) = @_;
# increment this counter every time the user requests a page
my $counter = $session->get('counter');
$counter ||= 0;
++$counter;
$session->set('counter', $counter);
sub (GET + /logout) {
# blow the user's session away
$session->expire();
# then redirect the user
$response->header('Location' => '/login');
return $response->finalize(301);
}
}
CONFIGURATION
The basic configuration for the session engine looks like this:
session:
state:
driver: Prancer::Session::State::Cookie
options:
session_key: PSESSION
store:
driver: Prancer::Session::Store::Storable
options:
dir: /tmp/prancer/sessions
The documentation for the state and store drivers will have more information about the specific options available to them.
METHODS
- id
-
This will return the session id of the current session. This is set and maintained by the session state package.
- has key
-
This will return true if the named key exists in the session object.
if ($session->has('foo')) { print "I see you've set foo already.\n"; }
It will return false otherwise.
- get key [default]
-
The get method takes two arguments: a key and a default value. If the key does not exist then the default value will be returned instead. If the value that has been stored in the user's session is a reference then a clone of the value will be returned to avoid modifying the session in a strange way. Additionally, this method is context sensitive.
my $foo = $session->get('foo'); my %bar = $session->get('bar'); my @baz = $session->get('baz');
- set key value
-
The set method takes two arguments: a key and a value. If the key already exists in the session then it will be overwritten and the old value will be returned in a context sensitive way. If the value is a reference then it will be cloned before being saved into the user's session to avoid any strangeness.
my $old_foo = $session->set('foo', 'bar'); my %old_bar = $session->set('bar', { 'baz' => 'bat' }); my @old_baz = $session->set('baz', [ 'foo', 'bar', 'baz' ]); $session->set('whatever', 'do not care');
- remove key
-
The remove method takes one argument: the key to remove. The value that was removed will be returned in a context sensitive way.
- expire
-
This will blow the session away.