yii2: error saving sessions
hi all, im using the new version of the yii2 2.0.6 but i have this problem
i get this Error
[16-Aug-2015 19:18:46 UTC] PHP Fatal error: Uncaught exception ‘yii\base\ErrorException’ with message ‘Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/tmp)’ in Unknown:0 Stack trace: #0 [internal function]: yii\base\ErrorHandler->handleError(2, ‘Unknown: Failed…’, ‘Unknown’, 0, Array) #1 {main}
thrown in Unknown on line 0
and is because i use this configuration
'session' => [
'class' => 'yii\web\DbSession',
'readCallback' => function ($fields) {
return [
'expireDate' => Yii::$app->formatter->asDate($fields['expire']),
];
},
'writeCallback' => function ($session) {
return [
'user_id' => Yii::$app->user->id,
'ip' => $_SERVER['REMOTE_ADDR'],
'is_trusted' => $session->get('is_trusted', false),
'controllerCurrent' => Yii::$app->requestedAction->controller->id,
'actionCurrent' => Yii::$app->requestedAction->id,
'pageCurrent' => Yii::$app->id,
'is_guest' => Yii::$app->user->isGuest
];
}
]
in the common.php
in the localhost work Perfectly but in the remote i get the error
any have idea how can solve this ?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 67 (37 by maintainers)
Commits related to this issue
- Fixes #9438: yii\web\DbSession now relies on error handler to display errors — committed to yiisoft/yii2 by samdark 7 years ago
- Handle session callback custom fields before session closed (#9438, #13740, #15037) — committed to lubosdz/yii2 by lubosdz 5 years ago
- Fixes #9438, #13740, #15037: Handle DB session callback custom fields before session closed — committed to yiisoft/yii2 by lubosdz 5 years ago
This error happens when the writeCallback is trying to access something that has not been created yet. Such as the Yii::$app->user. This normally happens because of an uncaught error that causes the app to error out before the accessed object is setup.
To reproduce the error, set write callback to:
'writeCallback' => function($session) { return [ 'user_id' => \Yii::$app->user->id ]; }And create a controller action that executes the following: ArrayHelper(‘string’,null,[]);
I was having this same error too for the longest time and couldn’t figure out why. After spending hours tracing the code with the debugger I discovered the problem for me was in my writeCallback() function, like you inside that function I had several calls to $session->get(‘param_name’).
The problem is, the writeCallback function is called upon sessionClose(), and by the time it gets to my call to session->get() the session is already closed. Inside the session->get() function the first thing it does is open the session if it is not already open, so my session was getting reopened after it had been closed. Then when the script ends and the memory is freed up from my session object, session_destroy() is called and that’s when the error is triggered: “‘Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/tmp)’”, I believe because the session was still open?
See if commenting out your line: ‘is_trusted’ => $session->get(‘is_trusted’, false),
fixes your problem. I had to rework my read and write callback functions and once I eliminated the session->get calls everything worked OK. I don’t know if this is a bug or if its a result of me using the session class incorrectly, but I had to store the session params I needed in separate variables in memory and access them there in order to get the writeCallback to work. Maybe not the ideal solution but at least now it works. Hope it helps you.