Function to displace through inactivity

0

Well, in my project when the user logs in the system performs a function that creates a session with his email to authenticate it during the site:

Function:

function logaUsuario($email) {
     $_SESSION["usuario_logado"] = $email;
}

Functions that verify that the user is logged in:

function usuarioEstaLogado() {
return isset($_SESSION["usuario_logado"]);
}

function verificaUsuario() {
    if(!usuarioEstaLogado()) {

       header("Location: login.php");
       die();
    }
}

However, I need to cause the system to log off after a few minutes of inactivity, but keep the email saved. What would be the best way to "disengage" the user, but continue with your stored email?

    
asked by anonymous 12.08.2017 / 03:13

1 answer

1
function checkAtividade() {
    if(time() - $_SESSION['timestamp'] > 900) { // Subtrai timestamp atual com o armazenado em SESSION['timestamp']
        echo"<script>alert('Deslogado por inatividade!');</script>";
        unset($_SESSION['timestamp']);
        $_SESSION['logged_in'] = false;
        header("Location: " . login.php); // Redireciona para a pagina login.php
        exit;
    } else {
        $_SESSION['timestamp'] = time(); // Atualiza timestamp
    }
}

Change your roles:

function logaUsuario($email) {
    $_SESSION["usuario_logado"] = $email;
    $_SESSION['timestamp'] = time();
    $_SESSION['logged_in'] = true;
}

function usuarioEstaLogado() {
    return isset($_SESSION["logged_in"]);
}
    
12.08.2017 / 03:57