对于
protected function renewAuthStatus()
{
$session = Yii::$app->getSession();
$id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($this->idParam) : null;
var_dump($session->getHasSessionId());
var_dump($this->idParam);
var_dump($session->get($this->idParam));
var_dump($id);
if ($id === null) {
$identity = null;
} else {
/* @var $class IdentityInterface */
$class = $this->identityClass;
$identity = $class::findIdentity($id);
}
我的输出为 bool(true) string(4) "__id" int(432) int(432)
$session = Yii::$app->getSession();
这个 $session , 得看配置
打开@common/config/main-local.php 看一下你的session的配置
'session' => [
'class' => 'yii\redis\Session',
fecshop 默认推荐使用redis,你的session的class是否是'yii\redis\Session'
对于上面的session值的写入,是在账户login的时候,还是 yii/web/User.php文件,我给你罗列下代码
登录函数
public function login(IdentityInterface $identity, $duration = 0)
{
if ($this->beforeLogin($identity, false, $duration)) {
$this->switchIdentity($identity, $duration);
$id = $identity->getId();
$ip = Yii::$app->getRequest()->getUserIP();
if ($this->enableSession) {
$log = "User '$id' logged in from $ip with duration $duration.";
} else {
$log = "User '$id' logged in from $ip. Session not enabled.";
}
Yii::info($log, __METHOD__);
$this->afterLogin($identity, false, $duration);
}
return !$this->getIsGuest();
}
里面有一行代码:$this->switchIdentity($identity, $duration);
这个函数的代码:
public function switchIdentity($identity, $duration = 0)
{
$this->setIdentity($identity);
if (!$this->enableSession) {
return;
}
/* Ensure any existing identity cookies are removed. */
if ($this->enableAutoLogin && ($this->autoRenewCookie || $identity === null)) {
$this->removeIdentityCookie();
}
$session = Yii::$app->getSession();
if (!YII_ENV_TEST) {
$session->regenerateID(true);
}
$session->remove($this->idParam);
$session->remove($this->authTimeoutParam);
if ($identity) {
$session->set($this->idParam, $identity->getId());
if ($this->authTimeout !== null) {
$session->set($this->authTimeoutParam, time() + $this->authTimeout);
}
if ($this->absoluteAuthTimeout !== null) {
$session->set($this->absoluteAuthTimeoutParam, time() + $this->absoluteAuthTimeout);
}
if ($this->enableAutoLogin && $duration > 0) {
$this->sendIdentityCookie($identity, $duration);
}
}
// regenerate CSRF token
Yii::$app->getRequest()->getCsrfToken(true);
}
这个函数里面有一行:
$session->set($this->idParam, $identity->getId());
也就是这里把这个id的值塞进去的,你可以退出下账户,然后重新登录,打印一下这个$identity->getId()
的值,是否是int。
大致说的差不多了,你自己排查下原因把。