Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session_write_close(): Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) #13740

Closed
miradnan opened this issue Mar 9, 2017 · 9 comments
Assignees

Comments

@miradnan
Copy link

miradnan commented Mar 9, 2017

What steps will reproduce the problem?

I had 2.0.8 and I upgraded to 2.0.11

What is the expected result?

It should allow me to browse the app instead it started throwing error

What do you get instead?

session_write_close(): Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/lib/php/session)

screen shot 2017-03-09 at 9 02 40 pm

Additional info

Q A
Yii version 2.0.11
PHP version 7.0.15
Operating system Ubuntu 14
@samdark
Copy link
Member

samdark commented Mar 9, 2017

Do you have permissions allowing your webserver user to access /var/lib/php/session?

@yii-bot
Copy link

yii-bot commented Mar 9, 2017

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

@miradnan
Copy link
Author

miradnan commented Mar 9, 2017

Yes. I gave it 777 even though it doesn't work. I have 2.0.8 working on the same machine and PHP config without any issues.

@samdark
Copy link
Member

samdark commented Mar 9, 2017

Well, "failed to write session data" means exactly what it's saying i.e. file could not be written by the user running PHP process. 2.0.8 was ignoring the fact and continue running with empty session. Current version throws this error to warn developer that it's not OK and session, in fact, isn't written.

You should check for permissions again and make sure that the user (usually www-data) can actually write to that directory.

@samdark samdark closed this as completed Mar 9, 2017
@miradnan
Copy link
Author

miradnan commented Mar 9, 2017 via email

@samdark samdark reopened this Mar 9, 2017
@samdark
Copy link
Member

samdark commented Mar 9, 2017

Hmm...

My sessions are stored in MySQL database.

But

session_write_close(): Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/lib/php/session)

That doesn't seem to make sense. Do you have right config file loaded?

@samdark samdark self-assigned this Mar 9, 2017
@miradnan
Copy link
Author

miradnan commented Mar 10, 2017

Yes. The same config was working absolutely fine with 2.0.8. I upgraded and this issue came up.

Here is my sessions config.
screen shot 2017-03-10 at 9 24 33 am

File common\db\DbSession contains

namespace common\db;

use Yii;
use yii\db\Connection;
use yii\db\Query;
use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\web\MultiFieldSession;
use yii\web\ErrorHandler;
use yii\db\Exception;
use common\helpers\RequestHandler;
use common\components\BrowserComponent;

class DbSession extends \yii\web\DbSession {

    /**
     * Device Type 1
     */
    const DEVICE_TYPE = 1;

    /**
     *
     * @var type 
     */
    public $executed = false;

    /**
     * Session write handler.
     * Do not call this method directly.
     * @param string $id session ID
     * @param string $data session data
     * @return boolean whether session write is successful
     */
    public function writeSession($id, $data) {
        if ($this->executed) {
            return $this->executed;
        }
        $this->executed = true;

        // exception must be caught in session write handler
        // http://us.php.net/manual/en/function.session-set-save-handler.php
        try {
            $query = new Query;
            $exists = $query->select(['id'])
                    ->from($this->sessionTable)
                    ->where(['id' => $id])
                    ->createCommand($this->db)
                    ->queryScalar();
            $fields = $this->composeFields($id, $data);

            $fields['ip'] = RequestHandler::getIp();
            $fields['device_type'] = self::DEVICE_TYPE;
            $fields['device_info'] = BrowserComponent::getBrowser();

            if (!empty(Yii::$app->session['user']['id'])) {
                $fields['user_id'] = Yii::$app->session['user']['id'];
            }

            if ($exists === false) {
                $this->db->createCommand()
                        ->insert($this->sessionTable, $fields)
                        ->execute();
            } else {
                unset($fields['id']);
                $this->db->createCommand()
                        ->update($this->sessionTable, $fields, ['id' => $id])
                        ->execute();
            }
        } catch (\Exception $e) {
            $exception = ErrorHandler::convertExceptionToString($e);
            // its too late to use Yii logging here
            error_log($exception);
            echo $exception;

            return false;
        }

        return true;
    }

}

SQL Structure

--
-- Table structure for table `system_sessions`
--

CREATE TABLE `system_sessions` (
  `id` char(40) NOT NULL,
  `user_id` bigint(20) NOT NULL DEFAULT '0',
  `ip` varchar(25) DEFAULT NULL,
  `expire` int(11) DEFAULT NULL,
  `data` longblob,
  `device_type` tinyint(4) NOT NULL DEFAULT '0',
  `device_identifier` varchar(255) DEFAULT NULL,
  `device_info` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `system_sessions`
--
ALTER TABLE `system_sessions`
  ADD PRIMARY KEY (`id`),
  ADD KEY `user_id` (`user_id`);

@miradnan
Copy link
Author

miradnan commented Mar 10, 2017

@samdark - It works well if I don't override the writeSession method. The code is pretty much the same as in yii\web\DbSession. All I am doing here is adding additional fields to session record.

@samdark
Copy link
Member

samdark commented Mar 10, 2017

Looks similar to #9438 with same reason of failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants