PHP session expires before time

Asked

Viewed 3,315 times

4

I have a problem with a session time in PHP with Codeigniter. I was using the sessions in the IC and with so many problems I decided to look for a solution to use native sessions with the IC and at first it has already improved, but still it continues to fall the session before the predetermined time.

In the settings I set the sess_expiration 7200 (CI default value) and every time I don’t touch the system for a certain time (I’ve done several tests like 10min, 30min, 1h) and almost always falls when I update the page.

I played the code in Pastebin to make it easier to read it.

http://pastebin.com/aq6tfdnM

And inspecting the firebug, in Resouces->Cookies the Expires / Max-Age theoretically correct. print do firebug

  • Are your server’s date and Timezone settings correct? Note that the sessions are already expired (25 February) if we consider today’s date (23 July)

  • Yes @gmsantos ... if you notice when I asked this question, it was on February 25th. But detail, I still have problems with CI sessions....

  • I didn’t notice that detail, I thought it was a recent question

3 answers

1

Here’s my session job:

function sessionTimeout($time_out)//em minutos
{
    $time_out= 60*$time_out;//passa para segundos

    ini_set('session.gc-maxlifetime', $time_out);

    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > $time_out)) 
    {
        // último pedido foi a mais de $time_out atrás
        session_unset();     // unset $_SESSION variable for the run-time
        session_destroy();   // destroy session data in storage
    }
    $_SESSION['LAST_ACTIVITY'] = time(); // atualiza a hora da última atividade

    if (!isset($_SESSION['CREATED'])) 
    {
        $_SESSION['CREATED'] = time();
    } else if (time() - $_SESSION['CREATED'] > $time_out) 
    {
        // sessão começou a mais de $time_out atrás
        session_regenerate_id(true);    // muda o ID de sessão para a atual sessão e invalida o antigo ID de sessão
        $_SESSION['CREATED'] = time();  // atualiza altura que a sessão foi criada
    }
}

Based in this answer of SOEN.

  • For some reason, maybe the same as the AP, happens to me the same. Unable to solve so far.

0

Try going there in the codeigniter application config.php and changing these values

$config['sess_expiration']= 0; /* nunca expirar a sessão sózinha  */
$config['sess_expire_on_close'] = TRUE; /* finalizar sessão quando o navegador for fechado */

or you can try for . htaccess

php_value session.cookie_lifetime "99999999"

http://www.kinghost.com.br/wiki/doku.php/htaccess

  • So I left open the page for a while without moving and after 1h I went to update and still dropped me. If I leave the sess_expiration equal to zero in practice will set to 2 years (60*60*24*365*2) and the sess_expire_on_close is not used in the class I showed. Have any more idea how I can do?

  • @Marcelodiniz I made a change in the answer...

  • So, nothing yet, it took me a while to answer because I left it in tests. I’ve been trying to solve this problem for a while now and it’s not easy. I guess it’s not something with server configuration because I’ve tried it on 3 different and the same thing happens.

0

Man, I’m tired of having trouble with the Codeigniter library Session. That’s why I developed mine based on PHP’s Native Session and never had a problem.

I opened a project on Github, follow the link: https://github.com/alamops/codeigniter_native_session

  • Only use as any Codeigniter library
  • Any implementation or error, just send a Pull Request or open an Issue
  • 1

    So, I’m kind of tired of having problems with the IC Sessions, so much so that I already looked for other Ibraries that implemented the native php sessions and still gave some problem. Then I’ll get a better look at that one of yours. Valew

  • Okay, fine. I had run a test in the native Codeigniter library, and the cookie didn’t save. Hence I decided to develop my own, based on Native Session with the main functions that Codeigniter offers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.