【问题标题】:Yii::app()->user->id sometimes gives empty resultYii::app()->user->id 有时会给出空结果
【发布时间】: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

标签: php heroku yii


【解决方案1】:

通过更改要存储在数据库表中的会话来解决。我不确定,但 Yii 在 Heroku 下的服务器上存储会话存在一些问题。 需要在“组件”下的配置数组中进行更改。

发件人:

        'session' => array(
            'class' => 'CHttpSession',
            'timeout' => 2400,
            'cookieParams' => array(
                    'httpOnly' => true,
                    'secure' => false,
            ),
    ),

收件人:

        'session' => array(
            'timeout' => 2400,
            'cookieParams' => array(
                    'httpOnly' => true,
                    'secure' => false,
            ),
            'class' => 'CDbHttpSession',
            'connectionID' => 'db',
            'sessionTableName' => 'lig_session',
    ),

【讨论】:

  • 如果您共享启动用户会话的代码部分会很有趣
猜你喜欢
  • 2012-06-17
  • 2014-03-14
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
相关资源
最近更新 更多