PHP $_SESSION 即使设置了值也返回空数组 [英] PHP $_SESSION Returning Empty Array Even When Values are Set

查看:51
本文介绍了PHP $_SESSION 即使设置了值也返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to diagnose a PHP problem. I'm passing values to a $_SESSION array, 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.

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

Here's a simplified version of my code (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) {...}

重新创建错误:

  1. 加载页面.这里没问题;顶部的var_dump($_SESSION) 返回array(0) {},这是预期的,底部的返回一个三成员数组,也符合预期.
  2. 在查询字段中提交带有某些内容的表单(以失败验证).尽管在上一个页面加载时设置了 $_SESSION 变量,第一个转储仍然返回 array(0) {}.
  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 $_SESSION variables being set on the previous page load, the first dump still returns array(0) {}.

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

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_dump produces 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 $_SESSION array 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.

我还应该注意,会话 ID 在页面加载时是相同的,因此看起来会话状态从页面加载到页面加载都保持不变.

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.

预先感谢您的帮助.

推荐答案

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

Use this row at the top of each webpage:

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

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

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

这篇关于PHP $_SESSION 即使设置了值也返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆