php - Yii 还记得我功能吗?

标签 php yii

您好,我是 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/

相关文章:

gridview - Yii2 : Get Sum In footer of gridview

php - 根据 activeCheckBox、Yii 框架的值变化更新数据库

php - 如何使用 PHP 中的 log() 获取曲线上的点?

php - 如何在 laravel 中使用多个输入字段在 laravel 中实现搜索功能

php - 你如何在 MySQL 中使用 Yii 存储过程

Yii 1.1 - 创建带有验证的多步骤表单

javascript - Yii,从 Javascript 函数调用 Controller 函数

php - 管理面板的 htaccess 重写规则

php - 在 SQL Select 中返回零

php - 经常检查文档会让你成为一个糟糕的编码员吗?