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?
– smigol
Of course. When the time expires, the session is destroyed.
– Lollipop
cool @Lollipop, this code I should put in the file that checks whether the user is logged in or not?
– smigol
I updated, based on your comment.
– Lollipop