php - Yii2 custom login is not working -
i new in yii2, right working custom login, did code login when go in home page through index action didn't session there, here code
public function actionindex() { $session = yii::$app->session; if ($session->isactive) { echo 'sdsd'; die; } return $this->render('index'); } public function actionlogin() { if (!\yii::$app->user->isguest) { return $this->gohome(); } $model = new user(); //if ($model->load(yii::$app->request->post()) && $model->login()) { if ($model->load(yii::$app->request->post()) && $model->validate()) { $username = $_post['user']['username']; $password = $_post['user']['password']; $condition = "username = '".$username."' , password = '".md5($password)."' , status = '1' , usertype = '1' "; $logindata = user::find()->where($condition)->all(); if(count($logindata)>0) { $username = $logindata[0]->username; $userid = $logindata[0]->userid; $session = yii::$app->session; $session->set('username',$username); $session->set('userid',$userid); $this->redirect('index'); } else { yii::$app->session->setflash('error', 'username or password incorrect'); } } return $this->render('login', [ 'model' => $model, ]); }
even after login still consider me guaest user, yii::$app->user->isguest
changes need ?
you don't login user in yii app
you don't need set session must assign proper value function
return \yii::$app->getuser()->login($youruser, $yourrememberme ? $this->module->rememberfor : 0);
Comments
Post a Comment