Skip to content
Advertisement

Either session expires in the middle of work or Warning: session_cache_expire(): Session cache expiration cannot be changed when a session is active

Using PHP 8.0.6 on Windows 10 locally I received this error message:

Warning: session_cache_expire(): Session cache expiration cannot be changed when a session is active in C:pathsessions.php on line 5

I updated the same file on the live Linux server (PHP 8.0.9) and the issue doesn’t occur.

The relevant code:

session_cache_expire($session_length);

My first guess is that this might be a Windows specific issue however I just saw that other people accessing the live server were also encountering this issue.

The original intention of this line was that members were being signed out in the middle of their sessions because the expiration date was not being updated with requests from the client computers to the server. I added this line and suddenly the session cookie expiration dates were being updated however this error would randomly start occuring. How do I keep the session alive without this nonsense error occurring?

Advertisement

Answer

The documentation (php.net/manual/en/function.session-cache-expire.php) states:

“Thus, you need to call session_cache_expire() for every request (and before session_start() is called).”

I suspect this is your problem, but cannot be certain without seeing more of your code.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement