PHP session expires too early when using fetch requests

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP session expires too early when using fetch requests

    Hello everyone,

    I’m working on a small PHP web application where users stay logged in using $_SESSION. Normally, the session should last about 30 minutes of inactivity, but I noticed something strange when my page sends periodic fetch (AJAX) requests to the server every few minutes.

    Sometimes the user suddenly gets logged out even though those requests should technically keep the session active. The session cookie is still present in the browser, but $_SESSION seems to be empty when the request reaches PHP.

    My setup:
    • PHP 8.2
    • Default file-based session storage
    • session.gc_maxlifetime set to 1800

    Has anyone experienced sessions expiring or resetting when using background fetch requests like this? Is there a recommended way to keep the session alive or prevent PHP’s garbage collector from removing it too early?

    Thanks!
    PHP Developer | Learning sessions & AJAX
Working...
X