How can I logout a user from all open tabs automatically when user logs out in one of them?

How can I logout a user from all open tabs automatically when user logs out in one of them?

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

Stefan Bogdanescu

Founder & Senior Architect

How can I logout a user from all open tabs automatically when user logs out in one of them?

Your current architecture relies on a client-side heartbeat mechanism. You have a sessioncheck.php endpoint that returns true or false, and your JavaScript polls this endpoint every three seconds via AJAX. If the session expires, the script redirects to logout.php. This approach effectively refreshes all tabs because every tab executes its own local instance of this polling loop. When one tab detects a missing session token (or the server invalidates the cookie), that specific tab navigates away.

However, you are asking for a more synchronized behavior: when the user performs a logout action in one tab, the application state must change globally across all other open tabs immediately. Your current implementation cannot achieve this because the polling loop is decoupled from the actual logout event. The server does not know which tab initiated the logout until that specific tab's heartbeat happens again or it sends a request to invalidate the session.

To solve this, you need to move the "logout" state propagation from a periodic check to an immediate event broadcast. There are two primary architectural patterns to consider depending on your stack requirements.

Server-Side Session Invalidation with Broadcasts

The most robust method is to ensure that when a user clicks the logout button in any tab, the server immediately invalidates the session ID for all devices associated with that user. If you are using a framework like Laravel, you can leverage its event system or simply invalidate the session and clear the cookie on the backend.

However, simply clearing the session on the server isn't enough; the other tabs need to know immediately without waiting for their next 3-second heartbeat. You must push this state change to the client side.

If your application uses WebSockets (like Laravel Reverb or Pusher), this is straightforward. When the logout action triggers in Tab A:

  1. The backend invalidates the session.
  2. The backend broadcasts a specific event, such as user.logged_out, containing the user ID.
  3. Any other tab listening to that WebSocket channel receives the event and instantly redirects to logout.php.

If you are restricted to standard HTTP requests without WebSockets, you can simulate this by forcing a hard page reload or redirect immediately upon logout, but this relies on the specific browser handling of the session cookie across tabs. Modern browsers generally share cookies for the same domain within a single tab group, but they do not automatically propagate navigation events between separate tabs.

Client-Side Event Bus Approach

A lighter-weight alternative involves decoupling the heartbeat logic from the logout event entirely. You should stop relying solely on sessioncheck.php to drive the navigation. Instead, treat the session check as a passive validation layer and create an active event system for state changes.

You can use an event bus library (like Vue's $emit, React context, or a lightweight vanilla JS pub/sub like mitt) to handle this. When the user logs out in Tab A:

  1. Trigger a global custom event, e.g., window.dispatchEvent(new CustomEvent('global-logout')).
  2. Add an event listener in your footer script that catches this event and redirects immediately.
// In the logout handler (triggered by clicking logout button)
function performLogout() {
    // Perform backend logout logic
    $.post('logout.php', {}, function() {
        // Broadcast to all other tabs
        window.dispatchEvent(new CustomEvent('global-logout'));
        
        // Redirect current tab
        window.location.href = 'logout.php';
    });
}

// Listener in the footer script (runs on every tab)
window.addEventListener('global-logout', function() {
    window.location.href = 'logout.php';
});

Trade-offs and Limitations

The main pitfall with the client-side event bus approach is that it relies on the user having cookies enabled and JavaScript active in all tabs. If a tab has JavaScript disabled, it will not receive the event, and that specific tab might remain logged in until its next heartbeat check fails. This creates an inconsistent state where some tabs show "Logged In" while others show "Logged Out."

The server-side broadcast approach is superior because it ensures consistency regardless of client-side capabilities. However, it requires setting up a real-time communication channel, which adds complexity to your deployment (e.g., managing the WebSocket server).

Conclusion

Your current polling mechanism works for detecting expiration but fails to propagate explicit logout actions instantly. To achieve synchronization across tabs, you must introduce an event-driven model. Whether you choose a WebSocket broadcast from the backend or a custom client-side event dispatcher depends on your existing infrastructure and whether you can tolerate slight inconsistencies in environments with JavaScript disabled.

Our network