您好,我是 yii 的新用户,下面是我的 UserIdentiy 函数,请告诉我如何添加记住我的功能
public function authenticate()
{
$users = array();
if ($this->usertype == "registration")
{
$users = Login::model()->findByAttributes(array('email' => $this->username));
$users = $users->attributes;
}
if (empty($users)) $this->errorCode = self::ERROR_USERNAME_INVALID;
elseif (!empty($users['password']) && $users['password'] !== md5($this->password))
$this->errorCode = self::ERROR_PASSWORD_INVALID;
elseif (!empty($users['status']) && $users['status'] !== 1)
$this->errorCode = self::STATUS_NOT_ACTIVE;
else
{
$this->_id = $users->id;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
最佳答案
在 protected\config\main.php
中,配置数组存在于该数组中,请转到 component
索引。该 user
数组内部具有关联索引值 'allowAutoLogin'
必须具有 bool 值 true
所以它应该看起来像这样
'components' => array(
'user' => array(
// enable cookie-based authentication
'allowAutoLogin' => true,
),
...
您必须使用以下属性以及下面给出的登录方法才能轻松记住我。
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
登录模型类中的登录方法应该像这样
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username, $this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
这就是记住函数的核心代码
Yii::app()->user->login($this->_identity,$duration);
关于php - Yii 还记得我功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26674611/