TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Different session lifetime for different users
  • How can I set different session lifetime for different users?

    I get my session instance with Session::instance(). This method can only specify session adapter type, not configuration settings like "lifetime".

    There are no methods for Session object itself, which could adjust it's lifetime.

    I want users who click on "remember me" to have long session lifetime (multiple days) and those who don't to have session limited only to as long as browser is open (lifetime of zero).

    I have Kohana 3.

  • Kohana 3.. Latest?

    Put this file in application/classes/Session.php

    I've taken the code from Kohana_Session::instance() and I've added in an extra parameter (lifetime), and if it's set, it will override any value set in the config.

    <?php defined('SYSPATH') OR die('No direct script access.');
    
    class Session extends Kohana_Session {
    
        public static function instance($type = NULL, $id = NULL, $lifetime = NULL)
        {
            if ($type === NULL)
            {
                // Use the default type
                $type = Session::$default;
            }
    
            if ( ! isset(Session::$instances[$type]))
            {
                // Load the configuration for this type
                $config = Kohana::$config->load('session')->get($type);
    
                if ($lifetime !== NULL)
                {
                    // Override the lifetime configuration
                    $config['lifetime'] = $lifetime;
                }
    
                // Set the session class name
                $class = 'Session_'.ucfirst($type);
    
                // Create a new session instance
                Session::$instances[$type] = $session = new $class($config, $id);
    
                // Write the session at shutdown
                register_shutdown_function(array($session, 'write'));
            }
    
            return Session::$instances[$type];
        }
    }
    
  • @zelcs see modules/auth/config/auth.php 'lifetime' => 1209600,

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion