PHP already has a parameter session.gc_maxlifetime
, that can be changed.
Edit the PHP.ini:
session.gc_maxlifetime = 1800
This will cause the session to expire after 1800 seconds (30 minutes).
Check if there is a session:
So just check if there is a session:
<?php
if(isset($_SESSION['sua_sessao'])){
// Se houver sessão - OK
}else{
header('location: logout.php');
// Se não houver - Redireciona para logout.php
}
?>
The session will expire in 30 minutes and redirect to logout.php.
Problems and corrections:
Suppose a user has NEVER logged in, if he accesses the page will be redirected to logout.php, because he also has no session, which in my opinion would be wrong.
So create a cookie when you log in and check as follows:
<?php
//...
}else{
if(isset($_COOKIE['login'])){
// Se existe o cookie, que já indica que o usuário já se conectou alguma vez
header('location: logout.php');
// Redireciona para logout.
}else{
// Se o visitante caiu de paraquedas na página e nunca fez o login
header('location: login.php');
// Redireciona para o login.
}
?>
In my case, I put your piece of code in my init.php, which is the page that is always present in all the others, yes? My login file only runs when the user logs in.
– David Concha
@Ivcs I put in my init.php but it is not sending the user to logout.php. o
echo
of the code gave this:Início de sessão: 1454174519
Redirecionando em 30 segundos.
– David Concha
For this I should see the file structure. If init is on all pages you can put the code there. Check if the
logout.php
is in the same init folder, if do not change the path ofheader
. And make sure you reload or change a page after the 30-second time has passed, as php does not check this in real time, only when there is a refresh.– Leonardo
@Ivcs perfect friend :) put in init.php page Thanks :)
– David Concha