gpt4 book ai didi

redis - 如何修复 yii2 中的 redis session 它确实登录了我的用户

转载 作者:可可西里 更新时间:2023-11-01 11:46:03 24 4
gpt4 key购买 nike

我在我的 Yii2 应用程序上安装了 redis,我正在使用 redis session ,可以完美地离线工作但是当尝试在添加用户详细信息后以在线用户身份登录时它会带来空白页面,直到我刷新超过两三次在它登录之前,如果我删除 redis session 就可以正常工作

//我的网络配置

'class'=>'app\components\DateFormat',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => false,
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
//'connectionTimeout'=>null,

],
'session' => [

'class' => 'yii\redis\Session',

'timeout'=>600,


],
'errorHandler' => [
'errorAction' => 'site/error',
],



//我在 Controller 中的登录

 public function actionLogin()
{


if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}

$model = new LoginForm();
// $login = new \app\models\LoginDetails();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
//$log = \app\models\User::find()->where(['user_login_id' => $_POST['LoginForm']['username'], 'is_block' => 0])->one();
/*if (empty($log)) {
\Yii::$app->session->setFlash('loginError', '<i class="fa fa-warning"></i><b> Incorrect username or password. !</b>');
return $this->render('login', ['model' => $model]);
}*/

// $login->login_user_id = Yii::$app->user->identity->user_id;
$loginuser = Yii::$app->user->identity->user_id;

$user_type = Yii::$app->user->identity->user_type;


$currentSes = Yii::$app->db->createCommand("Select aca.activesessions_id, aca.activesemester_id,ses.batch_name from activeacademic aca
join sessions ses on ses.batch_id= aca.activesessions_id join semester sem on sem.semester_id=aca.activesemester_id
where programme_id=:prg AND aca.activestatus=1", [':prg' => Yii::$app->user->identity->program_id])->queryOne();
// print_r($currentSes);die();
if (!empty($currentSes)) {
// $ses = $currentSes['activesessions_id'];
// $sem = $currentSes['activesemester_id'];
Yii::$app->session->set('active_ses', $currentSes['activesessions_id']);
Yii::$app->session->set('active_sem', $currentSes['activesemester_id']);
/* $active_ses_name = Yii::$app->db->createCommand("Select batch_name from sessions where batch_id=:id",
[':id' => $ses])->queryOne()['batch_name']; */
Yii::$app->session->set('active_ses_name', $currentSes['batch_name']);

}
if ( $user_type == 'S') {


$studlogin = Yii::$app->db->createCommand("Select stu_master_id from stu_master where stu_master_user_id=:sd",[':sd'=>$loginuser])->queryOne();//StuMaster::find()->andWhere(['stu_master_user_id' => $loginuser])->one();
if(!empty($studlogin))
\Yii::$app->session->set('stu_id', $studlogin['stu_master_id']);
\Yii::$app->session->set('student_data',$studlogin);


} else if ( $user_type == 'E') {

$emplogin = Yii::$app->db->createCommand("select emp_master_id, emp_master_academic_department_id from emp_master where emp_master_user_id=:em",[':em'=>$loginuser])->queryOne();//EmpMaster::find()->andWhere(['emp_master_user_id' => $loginuser])->one();
if(!empty($emplogin)){
\Yii::$app->session->set('emp_id', $emplogin['emp_master_id']);
\Yii::$app->session->set('emp_dept_id', $emplogin['emp_master_academic_department_id']);}
} else if ($user_type == 'A') {
\Yii::$app->session->set('admin_user', $loginuser);
} else {
if (isset(Yii::$app->user->id))
Yii::$app->user->logout();
\Yii::$app->session->setFlash('loginError', '<i class="fa fa-warning"></i><b> These Login credentials are Blocked/Deactive by Admin</b>');
return $this->render('login', ['model' => $model,]);
}
Yii::$app->db->createCommand()->insert('login_details',['login_user_id'=>$loginuser,'login_status'=>1,'login_at'=>date('Y-m-d h:i:s'),'user_ip_address'=> $_SERVER['REMOTE_ADDR']])->execute();
return $this->goBack();


} else {
return $this->render('login', [
'model' => $model,
]);
}
}


//错误日志

 PHP Recoverable Error – yii\base\ErrorException
session_regenerate_id(): Failed to create(read) session ID: user (path: )

最佳答案

我认为你应该在 session 部分添加redis的配置,试试这个:

    'session' => [
'class' => 'yii\redis\Session',
'redis' => 'redis'
'timeout'=>600,
],

或者这个:

    'session' => [
'class' => 'yii\redis\Session',
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
'timeout'=>600,
],

在第二个示例中,您可以更改商店配置,例如虚拟数据库:

'database' => 2

或其他配置。

关于redis - 如何修复 yii2 中的 redis session 它确实登录了我的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902331/

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