未定义的变量:$_SESSION

2022-08-30 14:05:19

我在核心 CakePHP 文件中遇到错误,当它尝试引用从未设置或未设置的会话时():E_NOTICEcake/libs/cake_session.php line 372

function read($name = null) {
    if (is_null($name)) {
        return $this->__returnSessionVars();
    }
    if (empty($name)) {
        return false;
    }
    $result = Set::classicExtract($_SESSION, $name);
}

我已经搜索了我的代码(在app/目录中),但我找不到对或的引用。我错过了什么吗?$_SESSIONsession_destroy

当我尝试运行任何单元测试时,会出现此错误。这是...正常?我已经清除了该目录并将其替换为另一个(相同版本),以确保我没有无意中修改核心文件中的任何内容,但我仍然收到相同的错误。我不确定这是否只是框架中的缺陷还是其他原因。cake/

编辑

以下是在命令行上运行测试的结果:

Welcome to CakePHP v1.3.11 Console
---------------------------------------------------------------
App : app
Path: /var/www/program/app
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
Running app case models/owners_equity
E_NOTICE: Undefined variable: _SESSION in /var/www/program/cake/libs/cake_session.php on line 372
E_NOTICE: Undefined variable: _SESSION in /var/www/program/cake/libs/cake_session.php on line 372
ERROR->Unexpected PHP error [Undefined variable: _SESSION] severity [E_NOTICE] in [/var/www/program/cake/libs/cake_session.php line 372]
    in testGenerateOwnerWithdrawals
    in BalanceTestCase
    in /var/www/program/app/tests/cases/models/owners_equity.test.php

ERROR->Unexpected PHP error [Undefined variable: _SESSION] severity [E_NOTICE] in [/var/www/program/cake/libs/cake_session.php line 372]
    in testGenerateOwnerWithdrawals
    in BalanceTestCase
    in /var/www/program/app/tests/cases/models/owners_equity.test.php

答案 1

您需要确保在要使用超全局的每个PHP文件的顶部启动会话。喜欢这个:$_SESSION

<?php
  session_start();
  echo $_SESSION['youritem'];
?>

您忘记了会话帮助程序。

查看此链接 : book.cakephp.org/2.0/en/core-libraries/helpers/session.html


答案 2

事实证明,AppModel中有一些额外的代码搞砸了事情:

在和 :beforeFindafterFind

App::Import("Session");
$session = new CakeSession();
$sim_id = $session->read("Simulation.id");

我不知道为什么,但这就是问题所在。删除这些行解决了我遇到的问题。


推荐