NAME

Nile::Plugin::Session - Session manager plugin for the Nile framework.

SYNOPSIS

use DateTime;

# plugin session must be set to autoload in config.xml

# save current username to session
if (!$app->session->{username}) {
    $app->session->{username} = $username;
}

# get current username from session
$username = $app->session->{username};

# save time of the user first visit to session
if (!$app->session->{first_visit}) {
    $app->session->{first_visit} = time;
}

my $dt = DateTime->from_epoch(epoch => $app->session->{first_visit});
$view->set("first_visit", $dt->strftime("%a, %d %b %Y %H:%M:%S"));
    

DESCRIPTION

Nile::Plugin::Session - Session manager plugin for the Nile framework.

Plugin settings in th config file under plugin section. The autoload variable is must be set to true value for the plugin to be loaded on application startup to setup hooks to work before actions dispatch.

This plugin uses the cache module CHI for saving sessions. All drivers supported by the CHI module are supported by this plugin.

<plugin>

    <session>
        <autoload>1</autoload>
        <key>nile_session_key</key>
        <expire>1 year</expire>
        <cache>
            <driver>File</driver>
            <root_dir></root_dir>
            <namespace>session</namespace>
        </cache>
        <cookie>
            <path>/</path>
            <secure></secure>
            <domain></domain>
            <httponly></httponly>
        </cookie>
    </session>

</plugin>

For DBI driver configuration example:

<driver>
    <driver>DBI</driver>
    <namespace>session</namespace>
    <table_prefix>cache_</table_prefix>
    <create_table>1</create_table>
</driver>

The DBI create table example:

CREATE TABLE <table_prefix><namespace> (
   `key` VARCHAR(...),
   `value` TEXT,
   PRIMARY KEY (`key`)
)

The driver will try to create the table if you set create_table in the config and table does not exist.

cache()

$app->plugin->session->cache();

Returns the CHI cache object instance used by the session. All CHI methods can be accessed through this method.

get set compute remove expire is_valid add replace append clear purge get_keys exists_and_is_expired

$app->plugin->session->set($key, $data, "10 minutes");

# same as

$app->plugin->session->cache->set($key, $data, "10 minutes");

These methods are a proxy to the CHI cache object methods. See CHI for details about these methods.

id()

$id = $app->plugin->session->id();
$app->plugin->session->id($id);

Returns or sets the current session id. Session id's are auto generated.

sha_bits()

$bits = $app->plugin->session->sha_bits();

# bits: 1= 40 bytes, 256=64 bytes, 512=128 bytes, 512224, 512256 

$bits = 1;
$app->plugin->session->sha_bits($bits);

Returns or sets the current session id generator Digest::SHA sha_bits.

Bugs

This project is available on github at https://github.com/mewsoft/Nile.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Nile.

SOURCE

Source repository is at https://github.com/mewsoft/Nile.

SEE ALSO

See Nile for details about the complete framework.

AUTHOR

Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> Website: http://www.mewsoft.com

COPYRIGHT AND LICENSE

Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, https://github.com/mewsoft/Nile, http://www.mewsoft.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.