3

here´s an example with gc_maxlifetime but it worked for none of the session. variables I tried

<?php
session_start();
ini_set('session.gc_maxlifetime', 1500);

this used to work, until PHP 7.2.0 and now throws this warning:

Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in /in/nOv0L on line 3

enter image description here

https://3v4l.org/nOv0L

I checked the changelog but I can´t find the reason for it.

can anyone tell me what changed, and how I can work around it?


p.s. I know that I could do it like this:

 ini_set('session.gc_maxlifetime', 1500);
 session_start();

but that´s not really the point of my question.

musashii
  • 445
  • 6
  • 13
  • PHP devs in 7.2 add this warning. Before it this code do same, but without warning. As writte below this is mistake if called after `start_session`. – Enyby Aug 02 '18 at 20:11

2 Answers2

4

Even if there is no warning, changing the setting after the session has started will not have any effect.

The manual says

Garbage collection may occur during session start

so if you change the value after you start the session it will have no effect. This is also true for most other session parameters.

What you're probably experiencing is an artefact of other changes such as the addition of this message as a warning (all other sources I found like e.g. Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time mention it just as a "message" with no associated level).

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • "The meaning in this context is that any stored session that was saved more than gc_maxlifetime ago should be deleted." ([symfony docs](https://symfony.com/doc/current/components/http_foundation/session_configuration.html#configuring-garbage-collection)). So changing the the value after the start of the session would have an effect, or am I misunderstanding something? – musashii Jan 11 '18 at 15:30
  • 1
    The first sentence states that this is done when the session opens. Changing the value will not trigger the garbage collector and therefore will not collect any session based on the new settings – apokryfos Jan 11 '18 at 15:48
  • ah, I see. I just realized I didn´t really have a grasp on how the gc works. thank you :) – musashii Jan 11 '18 at 15:55
1

Well, make all configuration changes before you start the session:

ini_set('session.gc_maxlifetime', 1500);
session_start();
simon
  • 2,896
  • 1
  • 17
  • 22