I have a problem with the session in php as I assign a variable inactivity condition to the closing session?
I have this code:
$fechaGuardada = $_SESSION["ultimoacceso"];
$ahora = date("Y-n-j H:i:s");
$tiempo_transcurrido = (strtotime($ahora)-strtotime($fechaGuardada));
//comparamos el tiempo transcurrido
if($tiempo_transcurrido >= 10) {
//si pasaron 10 segundos o más
session_destroy(); // destruyo la sesión
header("Location: index.php"); //envío al usuario a la pag. de autenticación
//sino, actualizo la fecha de la sesión
}
The problem here is that you log in at given time, in this case it's 10 seconds.
How do I get the condition to run after an inactivity for 10 seconds?
You could add a last activity session timestamp and compare that to the time now. You just need to on a valid session check update the timestamp, this also needs to be added on login.
$fechaGuardada = strtotime($_SESSION["ultimoacceso"]);
$now = time();
if(($now - $fechaGuardada) >= 10) {
// Loggout
}
else {
// Logged in
$_SESSION['ultimoacceso'] = $now;
}