Set time to expire $_SESSION

Asked

Viewed 15,992 times

3

How to adjust the time to expire $_SESSION?

attempts..

Include in top of files (worked on offline localhost)

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();

Change direct on php.ini

session.gc_maxlifetime = 3600
session.cookie_lifetime = 3600

but none of the methods worked on the Online server

1 answer

5

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 unreliable.

Creating a simple code to drop 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.

  • doubt... when destroying Session the variables set in it will be lost, and the user will have to redo the process if it has started and stopped until the time expires, this?

  • Of course. When the time expires, the session is destroyed.

  • cool @Lollipop, this code I should put in the file that checks whether the user is logged in or not?

  • I updated, based on your comment.

Browser other questions tagged

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