The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CGIS - Session enabled version of CGI.pm

SYNOPSIS

    use CGIS;
    
    $cgi = new CGIS;

    # use $cgi the same way you use CGI.pm, in addition:

    # storing data in the session:
    $cgi->session("full_name", "Sherzod Ruzmetov");

    # reading data off the session
    $full_name = $cgi->session("full_name");

    # If no arguments passed to session(), it returns CGI::Session driver 
    # object:
    $session = $cgi->session();

DESCRIPTION

CGIS is a simple, session-enabled extension for Lincoln Stein's CGI. Instead of loading CGI, you load CGIS, and use it the way you have been using CGI, without any exceptions.

In addition, CGIS provides session() method, to support persistent session management accross subsequent HTTP requests, and partially overrides header() method to ensure proper HTTP headers are sent out with valid session data (cookies).

CGIS requires CGI::Session installed properly. Uses $CGITempFile::TMPDIRECTORY as the default location for storing session files. This variable is defined in Lincoln Stein's CGI. You can hardcode this value to point to some other location.

EXAMPLES

In session management enabled sites, you most likely send proper session cookie to the user's browser at each request. Instead of doing it manual, simply call CGIS's header() method, as you would that of CGI.pm's:

    print $cgi->header();

And you are guaranteed that proper cookie's sent out to the user's computer.

STORING DATA IN THE SESSION

    # store user's name in the session for later use:
    $cgi->session("full_name", "Sherzod Ruzmetov");

    # store user's email for later:
    $cgi->session("email", 'sherzodr@cpan.org');

READING DATA OFF THE SESSION

    my $full_name = $cgi->session("full_name");
    my $email     = $cgi->param('email');
    print qq~<a href="mailto:$email">$full_name</a>~;

GETTING CGI::Session OBJECT

For further performing more sophisticated oprations, you may need to get underlying CGI::Session object directly. To do this, simply call session() with no arguments:

    $session = $cgi->session();

    # now ref($session) == 'CGI::Session::File';

    # set expiration ticker:
    $session->expire("+10m");

    # delete the session for good:
    $session->delete();

For more tricks, consult with CGI::Session manual.

AUTHOR

Sherzod B. Ruzmetov <sherzodr@cpan.org>

COPYRIGHT

This library is free software. You can modify and/or distribute it under the same terms as Perl itself.

SEE ALSO

CGI::Session, CGI