Search code examples
phpsessionhandler

php7 - Custom SessionHandler raises session_write_close() warning


I upgraded my project from php5.6 to php7.0 and got from time to time a php warning about session_write_close(), showing me the correct path to my temp folder.

My project uses a custom database session handler which worked perfectly with the older php version.

The error is as follows:

Warning: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (C:\my\path) in Unknown on line 0

Here is my write() fucntion:

/**
 * @param string $id
 * @param string $data
 * @return bool
 * @throws Zend_Db_Adapter_Exception
 */
public function write($id, $data)
{
    $dateTime = date('Y-m-d H:i:s');
    $newDateTime = date('Y-m-d H:i:s', strtotime($dateTime . sprintf(' + %s seconds', $this->getMaxLifetime())));

    $sessionValues = array("id" => $id, "expires_at" => $newDateTime, "session_data" => $data);

    $sel = new Zend_Db_Select($this->_zdb);
    $sel->from(self::TABLE_NAME, array('id'))
        ->where('id = ?', $id);
    $sessionExists = (bool)$this->_zdb->fetchOne($sel);

    if($sessionExists) {
        $result = $this->_zdb->update(self::TABLE_NAME, $sessionValues, array('id = ?' => $id));
    } else {
        $result = $this->_zdb->insert(self::TABLE_NAME, $sessionValues);
    }

    if($result) {
        return true;
    } else {
        return false;
    }
}

Any ideas what I can do about it?


Solution

  • Ok, finally I found the solution:

    My database adapter inside my custom session handler returns 0 on an update on the session table which doesn't update any values.

    I returned the result of my query in the write()-function.

    This guy here explained the problem.

    The SessionHandlerInterface::write() function raises this warning in case it returns false.

    Maybe this helps somebody else as well.