How to get the Session Timeout in CodeIgniter?

2019-05-06 17:18发布

问题:

I am trying to run a function 5 minutes before a session times out. My session timeout is set to 7,200 in my config file. Is it possible to do this with CodeIgniter?

回答1:

I think you're looking for something like this:

$lastActivity = $this->session->userdata('last_activity');

$timeOut = time() - 7200 + 300; // now minus the the session 
                                   // timeout plus 5 minutes

if ($lastActivity <= $timeOut)
{
    /* This runs on or after the "5 minutes before mark", but won't run if the
       session has expired. After the session times out, based on the 
       $config['sess_expiration'] var, it's destroyed, as pointed out by
       coolgeek. */

    // ... Do some stuff
}

You would probably want to run this inside of a hook (see the hook docs here) so that it runs on every page load before or after controllers are run, depending on what you're trying to do.



回答2:

A session times out if there are no requests (page hits) within the timeout period. As such, a php solution will not solve the problem.

You need to put a javascript timer in your page that will count down from the time that the page was requested and take some action once it reaches the (timeout - 5 minutes) threshold.



标签: codeigniter