NAME

CGI::Easy::Session - Support unique session ID and session data in cookies

VERSION

This document describes CGI::Easy::Session version v2.0.1

SYNOPSIS

use CGI::Easy::Request;
use CGI::Easy::Headers;
use CGI::Easy::Session;

my $r = CGI::Easy::Request->new();
my $h = CGI::Easy::Headers->new();
my $sess = CGI::Easy::Session->new($r, $h);

if (defined $sess->{id}) {
    printf "Session ID: %s\n", $sess->{id};
} else {
    print "User has no cookie support\n";
}
printf "Permanent var 'a': %s\n", $sess->{perm}{a};
printf "Temporary var 'a': %s\n", $sess->{temp}{a};

$sess->{perm}{b} = 'data';
$sess->{temp}{answer} = 42;
$sess->save();                  # BEFORE $h->compose()

DESCRIPTION

Manage session for CGI applications.

Detect is user has cookie support. Generate unique session ID for each user. Store persistent and temporary (until browser closes) data in cookies.

This module will set cookies sid , perm and temp , so you shouldn't use cookies with these names if you using this module.

INTERFACE

new

$sess = CGI::Easy::Session->new($r, $h);

Take $r (CGI::Easy::Request object) and $h (CGI::Easy::Headers object) and create new CGI::Easy::Session object with these public fields:

id      STRING (unique session ID or undef if no cookie support)
perm    HASHREF (simple hash with scalar-only values)
temp    HASHREF (simple hash with scalar-only values)

You can both read existing session data in {perm} and {temp} and add/update new data there, but keep in mind overall cookie size is limited (usual limit is few kilobytes and it differ between browsers). After changing {perm} or {temp} don't forget to call save().

Complex data structures in {perm} and {temp} doesn't supported (you can manually pack/unpack them using any data serialization tool).

Will set cookie "sid" (with session ID) in 'Set-Cookie' header, which will expire in 1 YEAR after last visit.

Return created CGI::Easy::Session object.

save

$sess->save();

Set/update 'Set-Cookie' header with current {perm} and {temp} values. Should be called before sending reply to user (with $h->compose()) if {perm} or {temp} was modified.

Cookie "perm" (with hash {perm} data) will expire in 1 YEAR after last visit. Cookie "temp" (with hash {temp} data) will expire when browser will be closed.

Return nothing.

SUPPORT

Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at https://github.com/powerman/perl-CGI-Easy/issues. You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license. Feel free to fork the repository and submit pull requests.

https://github.com/powerman/perl-CGI-Easy

git clone https://github.com/powerman/perl-CGI-Easy.git

Resources

AUTHOR

Alex Efros <powerman@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2009- by Alex Efros <powerman@cpan.org>.

This is free software, licensed under:

The MIT (X11) License