NAME

MojoX::Session - Session management for Mojo

SYNOPSIS

my $session = MojoX::Session->new(
    store     => MojoX::Session::Store::DBI->new(dbh  => $dbh),
    transport => MojoX::Session::Transport::Cookie->new(tx => $tx),
    ip_match  => 1
);

$session->create(); # creates new session
$session->load();   # tries to find session

$session->sid; # session id

$session->data('foo' => 'bar'); # set foo to bar
$session->data('foo'); # get foo value

$session->data('foo' => undef); # works
$session->clear('foo'); # delete foo from data

$session->flush(); # writes session to the store
undef $session;    # same as above

DESCRIPTION

MojoX::Session is a session management for Mojo. Based on CGI::Session and HTTP::Session but without any dependencies except the core ones.

ATTRIBUTES

MojoX::Session implements the following attributes.

store

Store object

my $store = $session->store;
$session  = $session->store(MojoX::Session::Store::DBI->new(dbh => $dbh));

transport

Transport to find and store session id

my $transport = $session->transport;
$session
    = $session->transport(MojoX::Session::Transport::Cookie->new(tx => $tx));

ip_match

Check if ip matches, default is 0

my $ip_match = $session->ip_match;
$ip_match    = $session->ip_match(0);

expires_delta

Seconds until session is considered expired

my $expires_delta = $session->expires_delta;
$expires_delta    = $session->expires_delta(3600);

METHODS

MojoX::Session inherits all methods from Mojo::Base and implements the following new ones.

new

my $session = MojoX::Session->new(...);

Returns new L<MojoX::Session> object.

create

my $sid = $session->create;
$session->flush;

Creates new session. Puts sid into the transport. Call flush if you want to store it.

load

$session->load;
$session->load($sid);

Tries to load session from the store, gets sid from transport unless it is provided. If sesssion is expired it will loaded also.

flush

$session->flush;

Flush actually writes to the store in these situations: - new session was created (inserts it); - any value was changed (updates it) - session is expired (deletes it)

Flush is also called on object destruction.

sid

my $sid = $session->sid;

Returns session id.

data

my $foo = $session->data('foo');
$session->data('foo' => 'bar');
$session->data('foo' => 'bar', 'bar' => 'foo');
$session->data('foo' => undef);
# or
my $foo = $session->data->{foo};
$session->data->{foo} = 'bar';

Get and set values to the session.

clear

$session->clear('bar');
$session->clear;
$session->flush;

Clear session values. Delete only one value if argument is provided. Call flush if you want to clear it in the store.

expires

$session->expires;
$session->expires(123456789);

Get/set session expire time.

expire

$session->expire;
$session->flush;

Force session to expire. Call flush if you want to remove it from the store. Flush will be called also on object destruction and will automatically delete expired session from the store.

is_expired

Check if session is expired.

extend_expires

Entend session expires time. Set it to current_time + expires_delta.

SEE ALSO

CGI::Session, HTTP::Session

AUTHOR

vti, vti@cpan.org.

COPYRIGHT

Copyright (C) 2008, Viacheslav Tikhanovskii.

This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10.