<blockquote>
<p>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:</p>
<p>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).</p>
</blockquote>
<p>I think you answered your own question?  From: <a href="https://secure.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime" rel="nofollow noreferrer">https://secure.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime</a></p>
<blockquote>
<p><code>session.gc_maxlifetime</code> specifies the number of seconds after which data will be seen as 'garbage' and potentially cleaned up</p>
</blockquote>
<p>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.</p>
<p>I'm not aware of any way to <em>force</em> 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.</p>

CC BY-SA 3.0
2016-03-24T20:58:45.513Z
Yes I've looked further into how Craft's user session works and am now hooking into that service.
Russ Back
CC BY-SA 3.0
2016-03-18T23:43:55.113Z
2016-03-18T23:43:55.113Z
2020-06-17T08:56:27.903Z
Brad Bell
<p>I want to store a session value and have that cleared after a set timeout.</p>

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

<p>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: <code>craft()-&gt;httpSession-&gt;get('foo')</code>.</p>

<p>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).</p>

<p>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?</p>

<plugin-development><config><session>
Craft/Yii not garbage collecting session?
# Craft/Yii not garbage collecting session?

(Asked by: username_0 on 2016-03-18)

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?

**Tags:** plugin-development, config, session


---

## 1 answers

### Answer by username_1 on 2016-03-18. Score: 1 | Accepted answer

> 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.

> **Comments:**
> **username_0**: Yes I've looked further into how Craft's user session works and am now hooking into that service.  

---

