We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to set the session time out in phalcon.

I am using Phalcon\Session\Adapter\Files for starting the session ,setting the session and removing the session after loggingout. But i want to set the session time ie the session shoulg automatically removed/deleted after certain time. How to do it? Please Help!!

(cookie) Session lifetime is defined per adapter you use. So in case of Files adapter, it will read ini config from your app server (check php.ini or so).

Ok..that works for my local server.....what i have to do if my project went live.

Do the same for the life server ? :)

Or ask your server admin to do it if you do not have access?

@jaikumar002 But wait a moment, you expect that some internal mechanism will trigger and delete session while user is still active on your app? Like a stopwatch basically, let's say no matter what you do on site, you have 10 minutes countdown timer after which your session terminates automatically.

That won't happen. That's whole another perspective, HTTP cookies and web app sessions are made for a different purpose.

ya..but i have multiple applicatios run on my server, so i cannot set the session expiry time same for all.

Most of the hosting companies I work with allow folder based configuration. For example one folder can be PHP 7 and other can be PHP 5.4.

Just ask your administrator to help you if you cant do it yourself or dont have access.



65.0k
Accepted
answer

ok...but for now i had done this.... this will run every time when the user clicks a new action

    if ($this->session->__isset('auth')) {
        $time = time() - $auth['time'];
        if ($time >= 20 && !$auth['remember']) {

            $this->session->set('auth', array(
                'id' => $auth['id'],
                'name' => $auth['name'],
                'user' => $auth['user'],
                'time' => time(),
                'remember' => $auth['remember'],
            ));
            $dispatcher->forward(array(
                'controller' => 'session',
                'action' => 'end'
            ));
            return false;
        } else {
            $this->session->set('auth', array(
                'id' => $auth['id'],
                'name' => $auth['name'],
                'user' => $auth['user'],
                'time' => time(),
                'remember' => $auth['remember'],
            ));
        }
    }