ini_set() refuses to change session settings because a session is already running
ini_set(): A session is active. You cannot change the session module's ini settings at this timeShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
Warning pointing at the line of ini_set('session....')session_start() runs earlier than the ini_set() callFix - 2
the warning appears even though no session_start() exists before the ini_set()session.auto_start is onFix - 3
warning when session_set_cookie_params() or session_save_path() runsSession cookie or save path set with the wrong call or at the wrong timeFix
- 1
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-cookieConfirmed by php.net
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_start2Fix
Set session.auto_start to 0 and start the session yourself, after your settings.
; php.ini
session.auto_start = 03Verify
Restart PHP or Apache, then confirm the value.
php -i | grep -i auto_startConfirmed by php.net
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/sessionsConfirmed by php.net
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
session_start() cannot open its session file because of Permission denied
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions)
With the default files handler, PHP writes one sess_ file per visitor into session.save_path.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix