What is the best solution on how to handle session, using CWebUser or CHttpSession? Currently i use CWebUser’s setState method, and i want to save user login info in CWebUser and then after some duration the session will expire. The problem is, when i test it, it goes well with firefox, but for some reason that i dont know, it won’t work with chrome.
class AbUserIdentity extends CUserIdentity
{
public $id;
public $username;
public $password;
...
private function storeInfo($user)
{
$this->setState('hasLogin', true);
$this->setState('username', $user->uid);
$this->setState('nickname', $user->unick);
$this->setState('email', $user->umail);
$this->setState('status', $user->ustat);
$this->setState('lastLogin', date( 'Y-m-d H:i:s' ));
}
}
class LoginForm extends CFormModel
{
public $username;
public $password;
// public $verifyCode;
...
public function login()
{
if ( $this->_identity->errorCode === ABUserIdentity::ERROR_NONE ) {
$duration = 3600*3; // 3 hours
Yii::app()->user->login( $this->_identity, $duration );
return true;
} else {
return false;
}
}
}
I see. But if the two class use $_SESSION mechanism, makes me wonder if the other is another solution, i am probably wrong. Anyhow thanks a lot for pointing that out.
I also read something about cookie i think. Somehow i don’t understand yet. And also the login part
You should have nothing in the cookie but the user id which is set by Yii using CWebUser::setId() and can be retrieved using CWebUser::getId() so this means you have no reason to use setState in the first place.
setState is used only if for example you use autologin and you want some data to be available in cookie when autologin happens, but again, having the user identifier in cookie is all you need to later query the database and have access to info.
You should use Yii::app()->session to store the user data, and you should hook into afterLogin method in order to populate user data from database into session.
Ha ha, right, i think i was thinking to CUserIdentity::setState ?
Anyway, Yii::app()->session is more straight forward than setState and avoids the above confusion in general. My thoughts still stands for the approach
After took some times, i now knew how to solve this one. My problem actually was to get the login form appeared after some duration. After i read all your great responses, then i tried to tweak my codes with a couple of time and tests, i could see my desired result. After that i can share the results here as i promised.
<?php
class LoginForm extends CFormModel
{
public $username;
public $password;
public $verifyCode;
private $_identity;
...
/**
* Logs in the user using the given username and password in the model.
*
* @return boolean whether login is successful
*/
public function login()
{
if ( $this->_identity->errorCode === ABUserIdentity::ERROR_NONE ) {
$duration = Yii::app()->params['sessionTimeout']; // 3 hours
Yii::app()->user->login( $this->_identity, $duration );
return true;
} else {
return false;
}
}
}
And that’s it. Thanks a lot to sleptor and twisted1919, you’re all great.