0
I am logging the data into the database table whenever there is a login, but I intend to do update
at the time of logout
.
To do the Insert, do it in by validating the user login:
if(isset($resultado)){
$_SESSION['usuarioId'] = $resultado['id'];
$_SESSION['usuarioNome'] = $resultado['nome'];
$_SESSION['usuarioNiveisAcessoId'] = $resultado['niveis_acesso_id'];
$_SESSION['usuarioEmail'] = $resultado['email'];
$_SESSION['usuarioSenha'] = $resultado['senha'];
$data_hora = date("Y-m-d H:i:s");
$_SESSION['acesso'] = $data_hora;
$tempolimite = 2;
$_SESSION['registro'] = time();
$_SESSION['limite'] = $tempolimite;
$teste1 = $_SESSION['usuarioId'];
$teste2 = $_SESSION['usuarioNome'];
$teste3 = $_SERVER["REMOTE_ADDR"];
$queries = "INSERT INTO raddb.sessoes (iduser, user, data, ip) VALUES ('$teste1', '$teste2', '".date('Y-m-d H:i:s')."', '$teste3')";
$teste = mysqli_query( $conn, $queries);
Where I created the Session
with the login date this way:
$data_hora = date("Y-m-d H:i:s");
$_SESSION['acesso'] = $data_hora;
On the page sair
I’m doing it this way:
session_start();
require("conexao.php");
date_default_timezone_set('Europe/Lisbon');
$teste1 = $_SESSION['usuarioId'];
$teste2 = $_SESSION['acesso'];
$date = strtotime($teste2);
$teste3 = date('Y-m-d H:i:s', $date);
$sql = "UPDATE raddb.sessoes SET datafim = '".date('Y-m-d H:i:s')."' WHERE iduser = $teste1 AND data = $teste3";
$teste = mysqli_query( $conn, $sql);
unset(
$_SESSION['usuarioId'],
$_SESSION['usuarioNome'],
$_SESSION['usuarioNiveisAcessoId'],
$_SESSION['usuarioEmail'],
$_SESSION['usuarioSenha']
);
$_SESSION['logindeslogado'] = "Deslogado com sucesso";
//redirecionar o usuario para a página de login
header("Location: ./index.php/login");
I am putting in Where the date condition (type datetime) of the login because if put only the condition iduser = $teste1
ago update
to every line that has that id
. For this not to happen I have to have the condition of the date and time of login, but with both conditions does not update
table. On the exit page I am converting the variable $teste2 = $_SESSION['acesso'];
for datetime
, but even so does the update
I made the change you suggested and on the exit page I tried keeping everything as I have in question and without this part of code
$date = strtotime($teste2); 
 $teste3 = date('Y-m-d H:i:s', $date);
, but does not update at the same date and time of the end– Bruno
@Bruno, you are passing the variable update
$teste2
for the date value?– Victor Carnaval
I’m doing it like this at Where:
WHERE iduser = $teste1 AND data = $teste2"
, but still does not update– Bruno
has already worked... What was the difference in updating your response
– Bruno
@Bruno, I added the explanation to the answer itself.
– Victor Carnaval
can help in this question link
– Bruno