Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ini_set() refuses to change session settings because a session is already running

Errorini_set(): A session is active. You cannot change the session module's ini settings at this time

Short answer

The setting was changed after the session had already started. Session options such as the save path and cookie lifetime are read when the session starts, so they must be set before session_start(), or passed to it as an options array. Find the code that starts the session first, or turn off session.auto_start.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place that starts a session, and check whether the server starts one by itself.

    grep -rn "session_start" --include=*.php .
    php -i | grep -i "session.auto_start"
  2. 2Match what you see, then jump to its fix

    1. 1Warning pointing at the line of ini_set('session....')session_start() runs earlier than the ini_set() callFix
    2. 2the warning appears even though no session_start() exists before the ini_set()session.auto_start is onFix
    3. 3warning when session_set_cookie_params() or session_save_path() runsSession cookie or save path set with the wrong call or at the wrong timeFix

What does the error look like?

  • ini_set(): A session is active. You cannot change the session module's ini settings at this time

Cause 1 of 3

session_start() runs earlier than the ini_set() call

1Check

Another file, a bootstrap or a framework loader starts the session before your configuration line. session_status() tells you at that point whether a session is already active.

var_dump(session_status() === PHP_SESSION_ACTIVE);

2Fix

Make the setting before the first session_start(). For options that session_start() accepts, pass them in the options array without the session. prefix, which overrides the ini values for that call.

<?php
session_start([
    'cookie_lifetime' => 86400,
]);

3Verify

Reload the page. The warning is gone and the Set-Cookie header carries the lifetime you set.

curl -s -D - -o /dev/null http://localhost/ | grep -i set-cookie

Confirmed by php.net

Did this fix it?

Cause 2 of 3

session.auto_start is on

1Check

With session.auto_start enabled, PHP starts the session at request startup, before any of your code. The directive is INI_PERDIR, so it can only be set in php.ini, .htaccess or the server config.

php -i | grep -i auto_start

2Fix

Set session.auto_start to 0 and start the session yourself, after your settings.

; php.ini
session.auto_start = 0

3Verify

Restart PHP or Apache, then confirm the value.

php -i | grep -i auto_start

Confirmed by php.net

Did this fix it?

Cause 3 of 3

Session cookie or save path set with the wrong call or at the wrong time

1Check

Both functions have to run before session_start(). When a framework or library starts the session first, your call comes too late.

grep -rn "session_set_cookie_params\|session_save_path" --include=*.php .

2Fix

Call them first, in the entry file, then start the session. Use the array form of session_set_cookie_params().

<?php
session_set_cookie_params([
    'lifetime' => 0,
    'path' => '/',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);
session_save_path('/home/example.com/sessions');
session_start();

3Verify

Load a page twice and confirm one session file is reused in the save path.

ls /home/example.com/sessions

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Configure sessions in one place, at the top of the entry script, before any library gets a chance to start one.
  • Guard session_start() with a check on session_status() so a second call does nothing.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.