gpt4 book ai didi

php - yii2 session 错误

转载 作者:行者123 更新时间:2023-11-29 02:13:51 26 4
gpt4 key购买 nike

我将 session 保存在 yii2 的 mysql 数据库中,但有时会出现错误 Ì 我认为 session 会在一段时间内破坏并显示错误消息,当我刷新页面时问题已解决

错误信息:

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}

配置 session 数据库

'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(),
];
}
],

最佳答案

您可以使用

控制 session 超时

'timeout' => 3600*4,// session 过期

'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

];
}
],

关于php - yii2 session 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com