1

I want to store a session value and have that cleared after a set timeout.

I’ve got PHP's session.gc_maxlifetime set at 60 seconds for development and am using Craft’s HttpSessionService to set a session value craft()->httpSession->set('foo').

I’m then checking that after 60 seconds and would expect it to be cleared by PHP’s session garbage collector but it’s still there: craft()->httpSession->get('foo').

I'm aware of how PHP's garbage collection works in that it's not run on every request (1/100 chance by default, defined by other PHP session settings).

What's the best way to force garbage collection on each session? Is there anything under the hood that we can leverage or do we need to write something custom?

1

I’m then checking that after 60 seconds and would expect it to be cleared by PHP’s session garbage collector but it’s still there:

I'm aware of how PHP's garbage collection works in that it's not run on every request (1/100 chance by default, defined by other PHP session settings).

I think you answered your own question? From: https://secure.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime

session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and potentially cleaned up

That means after 60 seconds the value in session has a 1/100 chance of being picked up by the garbage collector. Of course, you can affect that percentage via other settings, but there are some valid reasons why you would not want garbage collection to run on every request.

I'm not aware of any way to force the PHP garbage collector to run and clean certain values. I'd probably look at using cache based methods instead since you can set expiration times on cached items.

|improve this answer|||||
  • Yes I've looked further into how Craft's user session works and am now hooking into that service. – Russ Back Mar 24 '16 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.