Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

297
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!