php 未定义变量:$_SESSION

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9650090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:21:16  来源:igfitidea点击:

Undefined variable: $_SESSION

phpsessioncakephpe-notices

提问by Ian Hunter

I'm getting E_NOTICEerrors in a core CakePHP file when it tries to reference a never-set or unset session (cake/libs/cake_session.php line 372):

E_NOTICE在核心 CakePHP 文件中尝试引用从未设置或未设置的会话 ( cake/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);
}

I've done a search through my code (in the app/ directory) and I can't find references to $_SESSIONor session_destroy. Am I missing anything?

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

This error shows up when I try to run any unit tests. Is this...normal? I've cleared out the cake/directory and replaced it with another one (same version) just to make sure that I hadn't inadvertently modified anything in the core files, but I still get the same error. I'm not sure if this is just a flaw in the framework or something else.

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

EDIT

编辑

Here are the results of the test run on the command line:

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

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

采纳答案by Ian Hunter

Turned out there was some extra code in the AppModel that was messing things up:

结果发现 AppModel 中有一些额外的代码把事情搞砸了:

in beforeFindand afterFind:

beforeFindafterFind

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

I don't know why, but that was what the problem was. Removing those lines fixed the issue I was having.

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

回答by Ghostman

You need make sure to start the session at the top of every PHP file where you want to use the $_SESSIONsuperglobal. Like this:

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

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

You forgot the Session HELPER.

你忘记了会话助手。

Check this link : book.cakephp.org/2.0/en/core-libraries/helpers/session.html

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