Record logout time in the session table

Asked

Viewed 97 times

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

1 answer

1


Use the same session variable to store and retrieve the record in the database.

EDIT:

$queries = "INSERT INTO raddb.sessoes (iduser, user, data, ip) VALUES ('$teste1', '$teste2', '$data_hora', '$teste3')";

Note that your variable $data_hora is in single quotes, ie is being treated as string.

On the page sair change the following values:

$data_hora = $_SESSION['acesso'];

And update the update query:

$sql = "UPDATE raddb.sessoes SET datafim = '" . date('Y-m-d H:i:s') . "' WHERE iduser = $teste1 AND data = '$data_hora'";

Note that the variable $data_hora is in quotes again.

  • 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, you are passing the variable update $teste2 for the date value?

  • I’m doing it like this at Where: WHERE iduser = $teste1 AND data = $teste2", but still does not update

  • has already worked... What was the difference in updating your response

  • 1

    @Bruno, I added the explanation to the answer itself.

  • can help in this question link

Show 1 more comment

Browser other questions tagged

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