Heim  >  Artikel  >  Backend-Entwicklung  >  Yii 登录的时候 “记住我” 是怎么实现的呢?

Yii 登录的时候 “记住我” 是怎么实现的呢?

WBOY
WBOYOriginal
2016-06-06 20:43:461053Durchsuche

SESSION 和 COOKIE 的原理 我大致清晰。

我问了同事说最好配合表一起实现“记住我”,可是Yii这个没有用表的啊

就是想了解Yii的内部实现是怎么回事?

回复内容:

SESSION 和 COOKIE 的原理 我大致清晰。

我问了同事说最好配合表一起实现“记住我”,可是Yii这个没有用表的啊

就是想了解Yii的内部实现是怎么回事?

你用的是yii1么?如果是继续往下看
在yii的源码framework/web/auth/CWebUser.php里,登录方法如下
第二个参数duration就是是否记住的设置,yii默认生成的表单里会传入86400就是30天

<code class="lang-php">    public function login($identity,$duration=0)
    {
        $id=$identity->getId();
        $states=$identity->getPersistentStates();
        if($this->beforeLogin($id,$states,false))
        {
            $this->changeIdentity($id,$identity->getName(),$states);

            if($duration>0)
            {
                if($this->allowAutoLogin)
                    $this->saveToCookie($duration);
                else
                    throw new CException(Yii::t('yii','{class}.allowAutoLogin must be set true in order to use cookie-based authentication.',
                        array('{class}'=>get_class($this))));
            }

            if ($this->absoluteAuthTimeout)
                $this->setState(self::AUTH_ABSOLUTE_TIMEOUT_VAR, time()+$this->absoluteAuthTimeout);
            $this->afterLogin(false);
        }
        return !$this->getIsGuest();
    }
</code>

注意看saveToCookie方法,里面依赖于CSecurityManagerhashData方法,继续跟踪下去,可以发现依赖于CStatePersister存储的一些状态信息,而这个信息通常位于/path/to/app/protected/runtime/state.bin里。

简单的说,只要拿不到你的state.bin,拿不到你的程序路径,就没法伪造

cookie 记住帐号,数据库建一个记住帐号的表,当这台电脑访问的时候把cookie的帐号发给服务器,去查是否存在这个帐号,就跳过验证密码

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn