How to keep session alive without reloading page?

How to keep session alive without reloading page?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Keep Session Alive Without Reloading Page

Extending session lifetime by modifying session.gc_maxlifetime is a common first step, but it does not solve the core issue of inactivity expiration. When a user submits a form and spends ten minutes answering questions without interaction, the PHP session daemon (session.gc) still runs according to its schedule or the system's garbage collection timer. If that timer triggers while your user is idle, the session file is deleted from the disk regardless of how long you set gc_maxlifetime to be. The solution isn't just about setting a longer timeout; it's about actively refreshing the "last activity" timestamp whenever the user interacts with the interface.

To prevent automatic logout during long test sessions, you need to touch the session file or update the session data on every interaction. In PHP, the simplest way to achieve this without reloading the page is by calling session_regenerate_id(false) after processing a form submission or detecting user activity. This function regenerates the session ID and creates a new session file with the current timestamp, effectively resetting the garbage collection timer. Crucially, you must pass false as an argument; passing true would force a hard redirect to a new cookie path, breaking your existing login state.

Here is how you can implement this pattern in a typical test management system:

<?php
// Ensure session starts before any output
session_start();

// Logic to handle form submission or user interaction
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Process the form data
    $answer = $_POST['question_answer'];
    
    // Store the answer in the session
    $_SESSION['test_answers'][$current_question_id] = $answer;
}

// This is the critical line: regenerate ID without redirecting
// This updates the session file's access time, keeping it "fresh" for GC checks
session_regenerate_id(false); 

// Continue rendering the form with updated data
?>

However, relying solely on session_regenerate_id() inside a conditional block (like an if statement) can be inefficient if the page renders significantly slower than the user's input. It is often better to place this call early in the script, right after session_start(), ensuring it executes regardless of whether a specific event occurred. If your application architecture separates concerns strictly and you cannot touch every single file, consider using JavaScript setInterval or setTimeout to make small, benign AJAX requests (like fetching a timestamp) at regular intervals. This triggers a request-response cycle that PHP can intercept to refresh the session without requiring full page reloads.

There are important trade-offs to consider with this approach. Every call to session_regenerate_id() involves writing to disk and updating the session cookie in the browser, which introduces latency. In a high-concurrency environment, excessive regeneration calls could bottleneck your session storage layer. Additionally, if you use a file-based session handler (the default), frequent writes can fragment the disk or hit write limits on network-attached storage like NFS. If performance becomes an issue, switching to a persistent session store like Redis is recommended. With Redis, the session.gc behavior is handled by the PHP runtime but the underlying storage is much faster for these frequent updates.

Ultimately, the goal is to align the session's perceived inactivity with the actual user engagement. By touching the session state on every meaningful interaction, you ensure that users can take their time answering questions without being forced to re-authenticate mid-test.

Our network