Yii2 使用“自动登录”;“记住我”;不起作用

Yii2 使用“自动登录”;“记住我”;不起作用,yii2,yii2-basic-app,Yii2,Yii2 Basic App,我是Yii2的新手。我使用的是Yii 2基本模板 我在我的web应用程序上实现了“记住我”功能,但它并没有像我认为的那样工作。我可以成功登录(选中“记住我”复选框)。但在关闭浏览器并再次打开网站后,我没有登录,而是重定向到登录页面 我已在配置文件中将enableAutoLogin设置为true 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true,

我是Yii2的新手。我使用的是Yii 2基本模板

我在我的web应用程序上实现了“记住我”功能,但它并没有像我认为的那样工作。我可以成功登录(选中“记住我”复选框)。但在关闭浏览器并再次打开网站后,我没有登录,而是重定向到登录页面

我已在配置文件中将
enableAutoLogin
设置为
true

'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => true,
         'authTimeout' => 86400,
    ],

确保您的用户模型已经实现了
yii\web\IdentityInterface
,并且具有以下方法

  • findIdentity()
  • findIdentityByAccessToken()
  • getId()
  • getAuthKey()
  • validateAuthKey()

    使用yii\db\ActiveRecord

  • 使用yii\web\IdentityInterface

    class User extends ActiveRecord implements IdentityInterface
    {
        public static function tableName()
        {
            return 'user';
        }
    
        /**
         * Finds an identity by the given ID.
         *
         * @param string|integer $id the ID to be looked for
         * @return IdentityInterface|null the identity object that matches the given ID.
         */
        public static function findIdentity($id)
        {
            return static::findOne($id);
        }
    
        /**
         * Finds an identity by the given token.
         *
         * @param string $token the token to be looked for
         * @return IdentityInterface|null the identity object that matches the given token.
         */
        public static function findIdentityByAccessToken($token, $type = null)
        {
            return static::findOne(['access_token' => $token]);
        }
    
        /**
         * @return int|string current user ID
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return string current user auth key
         */
        public function getAuthKey()
        {
            return $this->auth_key;
        }
    
        /**
         * @param string $authKey
         * @return boolean if auth key is valid for current user
         */
        public function validateAuthKey($authKey)
        {
            return $this->getAuthKey() === $authKey;
        }
    }
    

    有关自动登录的更多信息,请参阅确保您的用户模型已实现
    yii\web\IdentityInterface
    ,并且具有以下方法

  • findIdentity()
  • findIdentityByAccessToken()
  • getId()
  • getAuthKey()
  • validateAuthKey()

    使用yii\db\ActiveRecord

  • 使用yii\web\IdentityInterface

    class User extends ActiveRecord implements IdentityInterface
    {
        public static function tableName()
        {
            return 'user';
        }
    
        /**
         * Finds an identity by the given ID.
         *
         * @param string|integer $id the ID to be looked for
         * @return IdentityInterface|null the identity object that matches the given ID.
         */
        public static function findIdentity($id)
        {
            return static::findOne($id);
        }
    
        /**
         * Finds an identity by the given token.
         *
         * @param string $token the token to be looked for
         * @return IdentityInterface|null the identity object that matches the given token.
         */
        public static function findIdentityByAccessToken($token, $type = null)
        {
            return static::findOne(['access_token' => $token]);
        }
    
        /**
         * @return int|string current user ID
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return string current user auth key
         */
        public function getAuthKey()
        {
            return $this->auth_key;
        }
    
        /**
         * @param string $authKey
         * @return boolean if auth key is valid for current user
         */
        public function validateAuthKey($authKey)
        {
            return $this->getAuthKey() === $authKey;
        }
    }
    

    有关自动登录的更多信息,请参阅我在型号中所做的操作:

                    if($this->rememberMe == 1){ 
                         setcookie (\Yii::getAlias('@site_title')."_admin_email", $this->username, time()+3600*24*4);
                         setcookie (\Yii::getAlias('@site_title')."_admin_password", $this->password, time()+3600*24*4);
                    }else{
                         setcookie (\Yii::getAlias('@site_title')."_admin_email", '');
                         setcookie (\Yii::getAlias('@site_title')."_admin_password", '');
                    }
    
    并在查看页面中使用以下方法检索:

    $cookies_email = isset($_COOKIE[Yii::getAlias('@site_title')."_admin_email"]) ? $_COOKIE[Yii::getAlias('@site_title')."_admin_email"] : '';?>
    <?php $cookies_password = isset($_COOKIE[Yii::getAlias('@site_title')."_admin_password"]) ? $_COOKIE[Yii::getAlias('@site_title')."_admin_password"] : '';?>
    
    $cookies\u email=isset($\u COOKIE[Yii::getAlias('@site\u title').“\u admin\u email”])$_COOKIE[Yii::getAlias(“@site\u title”)。“\u admin\u email”]:”;?>
    

    我的问题解决了:)

    我所做的是在模型中:

                    if($this->rememberMe == 1){ 
                         setcookie (\Yii::getAlias('@site_title')."_admin_email", $this->username, time()+3600*24*4);
                         setcookie (\Yii::getAlias('@site_title')."_admin_password", $this->password, time()+3600*24*4);
                    }else{
                         setcookie (\Yii::getAlias('@site_title')."_admin_email", '');
                         setcookie (\Yii::getAlias('@site_title')."_admin_password", '');
                    }
    
    并在查看页面中使用以下方法检索:

    $cookies_email = isset($_COOKIE[Yii::getAlias('@site_title')."_admin_email"]) ? $_COOKIE[Yii::getAlias('@site_title')."_admin_email"] : '';?>
    <?php $cookies_password = isset($_COOKIE[Yii::getAlias('@site_title')."_admin_password"]) ? $_COOKIE[Yii::getAlias('@site_title')."_admin_password"] : '';?>
    
    $cookies\u email=isset($\u COOKIE[Yii::getAlias('@site\u title').“\u admin\u email”])$_COOKIE[Yii::getAlias(“@site\u title”)。“\u admin\u email”]:”;?>
    

    我的问题解决了:)

    1。在您的用户模型中执行此操作(如果使用任何其他模型登录,则使用该模型)

    规则

    public$rememberMe=true

    并在模型中定义

    public function login()
    {
         if ($this->validate()) {
             return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
         }
         return false;
    }
    
    2.现在在查看页面中执行此操作

     <?= $form->field($model, 'rememberMe')->checkbox(['template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>"]) ?>
    

    1.在您的用户模型中执行此操作(如果使用任何其他模型登录,则使用该模型)

    规则

    public$rememberMe=true

    并在模型中定义

    public function login()
    {
         if ($this->validate()) {
             return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
         }
         return false;
    }
    
    2.现在在查看页面中执行此操作

     <?= $form->field($model, 'rememberMe')->checkbox(['template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>"]) ?>
    
    
    
    登录中使用“记住我”

    1-使用以下链接创建您的标识类:

    2-将此行添加到配置文件中:

    3-验证登录表单后使用此代码:

    /* If [[enableSession]] is `true`:
    - the identity information will be stored in session and be available in the next requests
    - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser
    - in case of `$duration > 0`: as long as the session remains active or as long as the cookie
      remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`.
      */
    $duration = $this->remember ? (30 * 24 * 3600) : 0;
    Yii::$app->user->login($user, $duration);
    
    登录中使用“记住我”

    1-使用以下链接创建您的标识类:

    2-将此行添加到配置文件中:

    3-验证登录表单后使用此代码:

    /* If [[enableSession]] is `true`:
    - the identity information will be stored in session and be available in the next requests
    - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser
    - in case of `$duration > 0`: as long as the session remains active or as long as the cookie
      remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`.
      */
    $duration = $this->remember ? (30 * 24 * 3600) : 0;
    Yii::$app->user->login($user, $duration);
    

    我对yii2不是很熟悉,但是这个功能在使用cookie时可以使用吗?如果是这样,您是否在关闭浏览器后使用浏览器选项删除Cookie?您是否确保在config/web.phpI中设置了
    cookieValidationKey
    。我不太熟悉Yii 2,但使用Cookie是否可以使用此功能?如果是,您是否在关闭浏览器后使用浏览器选项删除Cookie?您是否确保在config/web.php中设置了
    cookieValidationKey