PHP $_SESSION 即使设置了值也返回空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10519257/
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-24 22:22:56  来源:igfitidea点击:

PHP $_SESSION Returning Empty Array Even When Values are Set

phpsession

提问by kcbaker

I'm trying to diagnose a PHP problem. I'm passing values to a $_SESSIONarray, and those values are not taken by the array. I used var_dump($_SESSION)to see what was being returned and I get array (0){}. This is true even when I know the session is being set and values are being added to the array.

我正在尝试诊断 PHP 问题。我将值传递给一个$_SESSION数组,而这些值不被该数组采用。我过去常常var_dump($_SESSION)看到返回的内容,我得到array (0){}. 即使我知道正在设置会话并且将值添加到数组中也是如此。

Here's a simplified version of my code (index.php):

这是我的代码的简化版本(index.php):

<?php
session_start();
var_dump($_SESSION); 

if ($_REQUEST['query'] == "" or is_null($_REQUEST['query'])) {
$errormsg = "You must enter a search term to query.  Please try again.";
}

?>

<form name="product_search" id="product_search" method="post" action="index.php">
<input size="60" type="text" name="query" id="query" <?php echo "value=\"" .  $_REQUEST['query'] . "\" "; ?>/>
<?php if (isset($errormsg)) {echo "<span class=\"errormsg\">" . $errormsg . "</span>";} ?>
<input type="submit" class="searchBtn" value="Search"/>

<?php
    $_SESSION['query'] = 'foo';
    $_SESSION['search_field'] = 'man';
    $_SESSION['results'] = 'chu';
?>

var_dump($_SESSION); //Will dump array(3) {...}

To recreate error:

要重新创建错误:

  1. Load the page. No problem here; var_dump($_SESSION)at the top returns array(0) {}, which is expected, and the one at the botton returns a three-member array, also as expected.
  2. Submit the form with something in the query field (to defeat the validation). Despite the $_SESSIONvariables being set on the previous page load, the first dump still returns array(0) {}.
  1. 加载页面。这里没问题;var_dump($_SESSION)顶部返回array(0) {},这是预期的,而底部的返回一个三成员数组,也如预期的那样。
  2. 在查询字段中提交带有某些内容的表单(以失败验证)。尽管在$_SESSION上一个页面加载时设置了变量,但第一个转储仍然返回array(0) {}.

I simplified my code here to get at the problem, but I have another page that is returning a $_SESSION variable without difficulty (and the first var_dumpproduces a one-member array). Is the problem here that I'm submitting the form to itself, and if so, is there a way other than the $_SESSIONarray to pass values from one page load to the next? I've looked at the PHP documentation and searched high and low, but haven't caught anything that's helped.

我在这里简化了我的代码来解决这个问题,但是我有另一个页面可以毫无困难地返回一个 $_SESSION 变量(第一个var_dump生成一个成员数组)。这里的问题是我将表单提交给自己,如果是这样,除了$_SESSION数组之外,还有其他方法可以将值从一个页面加载传递到下一个页面吗?我查看了 PHP 文档并进行了高低搜索,但没有发现任何有用的东西。

I should also note that the session ID is the same across page loads, so it appears session state is being maintained from page load to page load.

我还应该注意,会话 ID 在页面加载中是相同的,因此看起来会话状态从页面加载到页面加载都在维护。

Thanks in advance for the help.

在此先感谢您的帮助。

回答by vuchkov

Use this row at the top of each webpage:

在每个网页的顶部使用这一行:

if (!isset($_SESSION)) { session_start(); }

if (!isset($_SESSION)) { session_start(); }

If you initialize $_SESSIONinto lower level script, then the `$_SESSION variable is not accessible in upper level.

如果您初始化$_SESSION为较低级别的脚本,则在较高级别无法访问 `$_SESSION 变量。

回答by Jeremy Lawson

Ensure that your server config allows session cookies.

确保您的服务器配置允许会话 cookie。

session.use_cookies = 1

The alternative is that the session id is passed around in the GET, and you would have to specifically append it to your form action, which you aren't doing. Hence, no session once the values are posted.

另一种方法是在 GET 中传递会话 ID,您必须专门将其附加到您的表单操作中,而您并没有这样做。因此,一旦发布了值,就没有会话。

回答by sreimer

Are sessions properly enabled? Here is the documentation for the PHP server session settings http://www.php.net/manual/en/session.configuration.php

会话是否正确启用?这是 PHP 服务器会话设置的文档 http://www.php.net/manual/en/session.configuration.php

To add to Jeremy's answer, you could also be denying cookies.

要补充 Jeremy 的回答,您也可以拒绝使用 cookie。

回答by shadyyx

Instead of SESSION You still can use a POST as You are using it anyway - simply pack the variables and their values to one (or more) hidden inputs (You can serialize the POST from one submit and insert it to a hidden input or You can create as much hidden inputs as much You have variables from first submit). Then in second submit (maybe a second step of multi-step form) You post new data and of course the data from the first submit...

代替 SESSION 您仍然可以使用 POST,因为您仍然可以使用它 - 只需将变量及其值打包到一个(或多个)隐藏输入(您可以从一次提交中序列化 POST 并将其插入隐藏输入,或者您可以创建尽可能多的隐藏输入你有第一次提交的变量)。然后在第二次提交(可能是多步表单的第二步)您发布新数据,当然还有第一次提交的数据......

But I still prefer session as it is easier (do not have to implement more code).

但我仍然更喜欢 session 因为它更容易(不必实现更多代码)。

Are You sure that the SESSION is not regenerating at each page load/submit? If You regenerate a session ID at page load/submit the old session (with its data) is lost and the data are inserted under new session ID...

您确定 SESSION 不会在每次页面加载/提交时重新生成吗?如果您在页面加载/提交时重新生成会话 ID,则旧会话(及其数据)将丢失,并且数据会插入到新会话 ID 下...

Also a recommendation: forget about the $_REQUEST unless You are sure it is totaly neccessary and rather use only $_GET or $_POST to obtain the data from the right channel...

还有一个建议:忘记 $_REQUEST 除非你确定它是完全必要的,而只使用 $_GET 或 $_POST 从正确的渠道获取数据......

回答by ymakux

session.cookie_secure = 1 may cause this issue

session.cookie_secure = 1 可能会导致这个问题

UPD: check session.cookie_secure is set to 0 for non-secure connection.

UPD:检查 session.cookie_secure 是否设置为 0 以表示非安全连接。

ini_set('session.cookie_secure', '0')

Worked for me.

为我工作。

UPD: Similar question PHP session.cookie_secure: Disables sessions when set to true

UPD:类似问题PHP session.cookie_secure:设置为 true 时禁用会话

回答by Arunas

This is a little late to the game, and it might be that nobody encounters this configuration issue. In fact, I hope not.

这游戏有点晚了,可能没有人遇到这个配置问题。事实上,我希望不会。

My web host just moved all my sites to a new server. They royally messed up the file and directory permissions, along with the user that the webserver and php scripts are run as.

我的网络主机刚刚将我所有的网站都移到了新服务器上。他们彻底搞砸了文件和目录权限,以及运行 web 服务器和 php 脚本的用户。

It seems that the

看来,

session.auto_start = 1

flag results in a different user (and I can't tell which) trying to read the session file than if I set it to 0 and just use

标志导致不同的用户(我无法分辨是哪个)尝试读取会话文件,而不是将其设置为 0 并仅使用

session_start();

in my scripts.

在我的脚本中。

The amazing permissions issue is that the session file is written but can't be read. So auto-starting sessions are always empty.

令人惊奇的权限问题是会话文件已写入但无法读取。所以自动启动会话总是空的。