首頁  >  問答  >  主體

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

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

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

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

巴扎黑巴扎黑2749 天前460

全部回覆(2)我來回復

  • PHP中文网

    PHP中文网2017-04-10 14:58:14

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

        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();
        }
    

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

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

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-10 14:58:14

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

    回覆
    0
  • 取消回覆