r/PHPhelp • • Aug 24 '26

PHP Default Session Management

I need to manually configure user side cookie expiration and server side session data expiration separately. also I can't find a way to update existing session data expiration since I update the expiration value of the cookie in some requests to keep user logged in.

$sessiontime = (60 * 60) * 12; //12 hours in seconds

//make sure at least 12 hours session never touched by session GC.
ini_set('session.gc_maxlifetime', $sessiontime);

....

session_set_cookie_params($sessiontime);
session_start();


//refresh session cookie time whenever we get request.
setcookie(session_name(), session_id(), [
    'expires' => time() + $sessiontime,
]);

am I missing something?

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/AssistanceClean948 Aug 24 '26

> This means that changing gc_maxlifetime in some requests is ineffective - requests with a lower gc_maxlifetime still have a chance to delete the server-side data files based on the lower value.

I mean I set that maxlifetime in the begining of the request how it could be happen? assuming I have 1 web app in the current machine

2

u/allen_jb Aug 24 '26

As I mentioned, when session gc runs, it doesn't apply to specific sessions. It applies to all files in the session.save_path

The only thing gc_maxlifetime affects is the session gc process, if that runs. gc_maxlifetime is not a per-session value. It's not saved in the session data anywhere.

When session gc runs, it deletes all session data files (not just those related to the current request) in session.save_path whose file modification time is older than gc_maxlifetime.

Say you have 2 requests:

  • Request A: gc_maxlifetime = 1 hour
  • Request B: gc_maxlifetime = 12 hours

Unless you've modified the gc probability / divisor settings, both requests have an equal chance of running gc each time they are hit.

If request A runs session gc, it will clean up all of the sessions in session.save_path based on its gc_maxlifetime of 1 hour.