Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't extend session timeout in ZF2

I'm saving my session data on my database(mongodb), the customer asked that he want the system to be idle for 2 hours(he has videos on the site which may take 2 hours to finish, so he asked to set session timeout value as 2 hour). I did it as Zend Framework suggested. see my config.php below

return array(
    'env2_session' => array(
        'config' => array(
            'class' => 'Zend\Session\Config\SessionConfig',
            'options' => array(
                'name' => 'test',
                'remember_me_seconds' => 7200,
                'gc_maxlifetime' => 7200,           
            ),
        ),
        'mongo_handler' => array(
            'options' => array(
                'class' => 'Zend\Session\SaveHandler\MongoDBOptions',
                'collection' => 'sessions',
            ),
        ),
        'save_handler' => 'Env2Session/Mongo/Handler',
        'validators' => array(
            array(
                'Zend\Session\Validator\RemoteAddr',
                'Zend\Session\Validator\HttpUserAgent',
            ),
        ),
    ),
);

But this is not the correct code as I couldn't see the session extended. it still get timed out after 24 minutes.

It will work if I add 'cookie_lifetime' => 7200 after 'gc_maxlifetime' => 7200 But this get timedout even if I'm using the site.

I want the timeout only when the system is idle. Please get me some thing on this.

Here is the save handler code which I written in Module.php


    public function getServiceConfig()
        {
            return array(
                'factories' => array(
                    'Env2Session/Mongo/Handler' => function ($sm) {
                        $config = $sm->get('config');
                        $session = $config['env2_session'];

                        $mongo = $sm->get('Env2\Mongo');

                        $class = isset($session['mongo_handler']['options']['class'])  ? $session['mongo_handler']['options']['class'] : 'Zend\Session\SaveHandler\MongoDBOptions';
                        $options = array();
                        $options['collection'] = $session['mongo_handler']['options']['collection'];
                        $options['database'] = $config['db']['mongo']['db_name'];
                        $mongoOption = new $class($options);

                        $sessionSaveHandler = new MongoDB($mongo, $mongoOption);
                        return $sessionSaveHandler;
                    },
                    'Env2Session' => function ($sm) {
                        $config = $sm->get('config');
                        if (isset($config['env2_session'])) {
                            $session = $config['env2_session'];

                            $sessionConfig = null;
                            if (isset($session['config'])) {
                                $class = isset($session['config']['class'])  ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
                                $options = isset($session['config']['options']) ? $session['config']['options'] : array();
                                $sessionConfig = new $class();
                                $sessionConfig->setOptions($options);
                            }

                            $sessionStorage = null;
                            if (isset($session['storage'])) {
                                $class = $session['storage'];
                                $sessionStorage = new $class();
                            }
                            $sessionSaveHandler = null;
                            if (isset($session['save_handler'])) {
                                $sessionSaveHandler = $sm->get($session['save_handler']);
                            }

                            $sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);

                            if (isset($session['validator'])) {
                                $chain = $sessionManager->getValidatorChain();
                                foreach ($session['validator'] as $validator) {
                                    $validator = new $validator();
                                    $chain->attach('session.validate', array($validator, 'isValid'));
                                }
                            }
                        } else {
                            $sessionManager = new SessionManager();
                        }
                        Container::setDefaultManager($sessionManager);
                        return $sessionManager;
                    }
                ),
            );
        }

like image 609
Haneef Abdulla Avatar asked Mar 29 '26 20:03

Haneef Abdulla


1 Answers

I was struggling with the same issue for zend session timeouts. Finally I implemented own solution for this case. I set up the expiration time which is refreshed during each request. But if user is not active, the time will expire and session will be destroyed.

I put all tutorial how to do it here: http://blog.tulikowski.com/zend-framework-2-implementation-of-session-authentication-timeouts/

like image 197
zleek Avatar answered Mar 31 '26 09:03

zleek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!