3

I save session in mysql db in yii2 but have problem some times with error ، I think the session destroy in period of time and show the error message and when i refresh page that problem is solved

error message:

PHP Notice 'yii\base\ErrorException' with message 'Undefined property: app\components\User::$id' in D:\xampp\htdocs\MyTrip\app\components\User.php:39 Stack trace: #0 D:\xampp\htdocs\MyTrip\app\components\User.php(39): yii\base\ErrorHandler->handleError(8, 'Undefined prope...', 'D:\xampp\htdocs...', 39, Array) #1 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\User.php(296): app\components\User->afterLogin(Object(app\models\User), true, 2592000) #2 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\User.php(674): yii\web\User->loginByCookie() #3 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\User.php(188): yii\web\User->renewAuthStatus() #4 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\User.php(343): yii\web\User->getIdentity() #5 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\base\Component.php(132): yii\web\User->getId() #6 D:\xampp\htdocs\MyTrip\app\config\web.php(50): yii\base\Component->__get('id') #7 [internal function]: {closure}(Object(yii\web\DbSession)) #8 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\MultiFieldSession.php(104): call_user_func(Object(Closure), Object(yii\web\DbSession)) #9 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\DbSession.php(174): yii\web\MultiFieldSession->composeFields('lvvoa4a8n7jch5n...', '__flash|a:0:{}') #10 [internal function]: yii\web\DbSession->writeSession('lvvoa4a8n7jch5n...', '__flash|a:0:{}') #11 D:\xampp\htdocs\MyTrip\app\vendor\yiisoft\yii2\web\Session.php(187): session_write_close() #12 [internal function]: yii\web\Session->close() #13 {main}

config session db

'session' => [
        //'class' => 'yii\mongodb\Session',
        //'class' => 'yii\web\Session',
        'class' => 'yii\web\DbSession',
        'writeCallback' => function($session)
        {
            return [
                'user_id' => Yii::$app->user->id,
                'agent' => Yii::$app->request->getUserAgent(),
                'ip' => Yii::$app->request->getUserIP(),

                //'auth_key' => Yii::$app->security->generateRandomString(),
            ];
        }
    ],
Chinmay Waghmare
  • 5,368
  • 2
  • 43
  • 68
mohammad zahedi
  • 313
  • 3
  • 17

2 Answers2

1

You can control the session timeout using

'timeout' => 3600*4, //session expire

'session' => [
    //'class' => 'yii\mongodb\Session',
    //'class' => 'yii\web\Session',
    'class' => 'yii\web\DbSession',
    'writeCallback' => function($session)
    {
        return [
            'user_id' => Yii::$app->user->id,
            'agent' => Yii::$app->request->getUserAgent(),
            'ip' => Yii::$app->request->getUserIP(),
            //'auth_key' => Yii::$app->security->generateRandomString(),

            'timeout' => 3600*4, //session expire

        ];
    }
],
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • thanks, but have some problem , I have login user with auth_key when this problem is happen session is destroy in next refresh the page is problem solved with your soultion just the time is increase the time but the problem is still happend – mohammad zahedi May 30 '17 at 06:52
1

Timeout is defined in session. not in writeCallback

Except as a field in the database. This will not work as "session expire".

'session' => [
    //'class' => 'yii\mongodb\Session',
    //'class' => 'yii\web\Session',
    'class' => 'yii\web\DbSession', 
    'timeout' => 3600*4, //session expire
    'writeCallback' => function($session)
    {
        #code .... Database field
    }
],
user206
  • 1,085
  • 1
  • 10
  • 15