Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Session Timeout in CodeIgniter?

Tags:

codeigniter

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?

like image 873
Kaartikeyan R Avatar asked Dec 17 '22 16:12

Kaartikeyan R


2 Answers

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.

like image 121
Gavin Anderegg Avatar answered Jan 16 '23 05:01

Gavin Anderegg


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.

like image 31
coolgeek Avatar answered Jan 16 '23 07:01

coolgeek