Maaz Zindani

Loading the index…

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

session_start() fails because the session save handler cannot open

Your error

Warning: session_start(): Failed to initialize storage module: user (path: /var/lib/php/sessions)2 more lines it can show

Fix it fast

Most common cause, 1 of 2

The open() method of a custom session handler returned false

Repair the store connection the handler depends on, and make open() log the underlying exception before it returns false, so the next failure names its cause.

Fix
public function open(string $path, string $name): bool
{
    try {
        $this->pdo->query('SELECT 1');
    } catch (PDOException $e) {
        error_log('session store unreachable: ' . $e->getMessage());
        return false;
    }
    return true;
}

// bootstrap
session_set_save_handler(new App\Session\DbSessionHandler($pdo), true);
session_start();
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

PHP prints Failed to initialize storage module: <handler> (path: ...) when the save handler's open step fails. With user, the open() method of your custom handler returned false, usually because its database or Redis connection failed. With files and an empty path, open_basedir blocks the temp folder. Repair that connection or set session.save_path. Applies to PHP 8.2-8.6.

  • Applies to PHP 8.2, 8.3, 8.4, 8.5 and 8.6, PHP 7.4 (older wording only)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find the full warning in the log. The word after storage module: names the handler that failed. user means a handler registered in code; files with nothing after path: means the built-in handler had no folder it was allowed to use.

Run this first
sudo grep -rh "Failed to initialize storage module" /var/log/ | tail -n 3

Then find the line you see in this list and open its fix:

  1. 1The open() method of a custom session handler returned falseThe warning says storage module: user, and the app stores sessions in a database, Redis or another service through its own handler class
  2. 2session.save_path is empty and open_basedir excludes the temp folderThe warning says storage module: files (path: ) with an empty path, right after open_basedir restriction in effect. File(/tmp) is not within the allowed path(s)

What does the error look like?

  • Warning: session_start(): Failed to initialize storage module: user (path: /var/lib/php/sessions)
  • Warning: session_start(): Failed to initialize storage module: files (path: )
  • Session callback expects true/false return value

Cause 1 of 2

The open() method of a custom session handler returned false

You seeThe warning says storage module: user, and the app stores sessions in a database, Redis or another service through its own handler class

Check

Failed to initialize storage module: user means session_start() called open() on the handler given to session_set_save_handler() and got false back. The manual defines true as success and false as failure for that callback. Handlers that connect to their store inside open() report a refused connection or a bad password this way. On PHP 7.4 a return value that was not a bool also landed here, right after Session callback expects true/false return value; PHP 8.2 and later raise a TypeError for that case instead.

Check
<?php
// call the handler the app registers, with the values from the warning
$handler = new App\Session\DbSessionHandler($pdo);
var_dump($handler->open(session_save_path(), session_name()));

Fix

Repair the store connection the handler depends on, and make open() log the underlying exception before it returns false, so the next failure names its cause. Register the handler as an object: PHP 8.4 deprecated calling session_set_save_handler() with more than two arguments, and PHP 8.6 deprecates handler objects that lack create_sid() and validateId().

Fix
public function open(string $path, string $name): bool
{
    try {
        $this->pdo->query('SELECT 1');
    } catch (PDOException $e) {
        error_log('session store unreachable: ' . $e->getMessage());
        return false;
    }
    return true;
}

// bootstrap
session_set_save_handler(new App\Session\DbSessionHandler($pdo), true);
session_start();

Verify

open() now prints bool(true), and a request that starts a session leaves no storage warning in the log.

Verify
<?php
var_dump(session_start(), session_status() === PHP_SESSION_ACTIVE);

Confirmed by php.net

Did this fix it?

Cause 2 of 2

session.save_path is empty and open_basedir excludes the temp folder

You seeThe warning says storage module: files (path: ) with an empty path, right after open_basedir restriction in effect. File(/tmp) is not within the allowed path(s)

Check

With the built-in files handler and an empty session.save_path, PHP falls back to the system temporary directory and first runs that folder through the open_basedir check. When open_basedir lists only the site's own folders, that check fails, open() reports failure and the warning shows an empty path. Read the values through the web server, because the CLI may load another php.ini.

Check
<?php
var_dump(ini_get('session.save_path'), ini_get('open_basedir'), sys_get_temp_dir());

Fix

Give sessions their own folder inside the allowed paths, owned by the PHP user and closed to everyone else, and name it in session.save_path. The manual warns that a world-readable folder such as /tmp lets other users on the server read session files.

Fix
sudo mkdir -p /var/www/example/sessions
sudo chown www-data:www-data /var/www/example/sessions
sudo chmod 700 /var/www/example/sessions

; php.ini used by the web server
session.save_path = "/var/www/example/sessions"
open_basedir = "/var/www/example"

Verify

After a PHP restart, a page that starts a session creates a sess_ file in the new folder.

Verify
sudo ls -l /var/www/example/sessions | head -n 5

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Write custom handlers as classes that implement SessionHandlerInterface plus create_sid() and validateId(), the methods PHP 8.6 expects for strict mode.
  • Keep an error_log() call in every failure branch of open(), so the store's own message reaches the log.
  • Set session.save_path explicitly in every vhost or pool that also sets open_basedir.
  • Add a post-deploy smoke test that starts a session, writes a value and reads it back through the real store.

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 on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

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