Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
Logout after inactivity with php AND js

I have this php code to log out after 30 Minutes of inactivity in my index.php:

<?php 
    
    session_start(); /* Starts the session */

    if(!isset($_SESSION['UserData']['Username'])){
        header("location:login.php");
        exit;
    }
    else
    {
        if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) { 
        // last request was more than 30 minutes ago
        session_unset();     // unset $_SESSION variable for the run-time 
        session_destroy();   // destroy session data in storage
        header("location:login.php?session_timeout");
        exit;
        }
        $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
    }
?>

This code is on top of every protected page. As long as I reload this page the inactivity counter is updated, and also the logout does happen.

The problem is, all the other code in my webpage is written in js and jQuery, so the page never really get's refreshed, and so does the php code never executes. That leads to an not updated inactivity tiemer and I also don't get logged out.

Is there a possibility to change this behaviour?

Thanks!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda