Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda