【发布时间】:2016-07-24 06:00:08
【问题描述】:
托管在 Heroku 上 Yii 1.14 PHP 5.6
一个奇怪的错误,当 Yii::app()->user->id 有时返回 id 但有时返回空。相同的页面加载会导致这种奇怪的行为。
我在一个名为 AdminController.php 的父类中检查这一点
class AdminController extends CController
{
public $partnerCount;
public $vendorCount;
public $plantationMarkers;
public function init()
{
echo Yii::app()->user->id;
if(empty(Yii::app()->user->id)) {
echo 'User id empty: '. Yii::app()->user->id;
//$this->redirect(Yii::app()->createAbsoluteUrl('admin/auth/login'));
exit;
return false;
}
elseif(!Yii::app()->user->checkAccess(User::PARTNER)) {
$this->layout = 'column1';
$this->render('/auth/not-authorized');
return false;
}
$this->partnerCount = $this->getPartnerCount();
$this->vendorCount = $this->getVendorCount();
$this->plantationMarkers = $this->getPlantationMarkers();
return true;
}
因此,在此代码中,“用户 ID 为空:”在某些情况下会回显,而在其他情况下,我会得到 id。
关键是代码没有变化,这如何在某些时候工作而在其他时候不工作。
【问题讨论】:
-
确保与会话超时无关。然后您的登录凭据已过期
-
不是。如果我第二次或任何其他时间重新加载页面,则 id 值就在那里。问题仅出在 heroku 安装上,而我的本地安装工作正常。
-
只是一个旁注。你不应该评估 id 来检查用户是否登录,使用:Yii::app()->user->isGuest