Set time to expire $ _SESSION

2

How to adjust the time to expire $ _SESSION?

attempts ..

Include in top of files (worked in localhost offline)

ini_set('session.gc_maxlifetime', 3600); // 1 hora
ini_set('session.cookie_lifetime', 3600);
session_start();

And also

session_set_cookie_params(3600); // 1 hora
session_start();

Direct change in php.ini

session.gc_maxlifetime = 3600
session.cookie_lifetime = 3600

but none of the methods worked on the Online server

    
asked by anonymous 17.03.2015 / 20:31

1 answer

4

In general, I always use this form. Implementing a session timeout:

if (isset($_SESSION['ultima_atividade']) && (time() - $_SESSION['ultima_atividade'] > 3600)) {

    // última atividade foi mais de 60 minutos atrás
    session_unset();     // unset $_SESSION  
    session_destroy();   // destroindo session data 
}
$_SESSION['ultima_atividade'] = time(); // update da ultima atividade

session.gc_maxlifetime and session.cookie_lifetime are not trusted.

Creating a simple code to move off when the session expires:

     session_start(); 

     ini_set('session.use_trans_sid', 0);

     if (!isset($_SESSION['name'])){
       $_SESSION['name']="Guest";

     }

     if ($_SESSION['name']!="Guest"){
        $counter = time();

        if (!isset($_SESSION['count'])){
          $_SESSION['count']= $counter;
        }

        if ($counter - $_SESSION['count'] >= 3600 ){
header('Location: http://www.meusite.com.br/sair.php');

       }
        $_SESSION['count']= $counter;

    } 

Read more by clicking here.

    
17.03.2015 / 20:51