PHP 会话变量未传递到下一页

问题描述:

我知道这个标题与 1000 多个与某个问题相关的其他帖子重复,如果解决方案已经存在,我提前道歉.相信我,我浏览了大量关于此主题的帖子,并尝试了所有规定的解决方案,但都没有成功.

I know this title is duplicate to 1000's of other posts related to an issue and I apologize in advance if the solution is out there. Trust me I went through tons of posts regarding this topic and tried all the solutions prescribed, but have had no luck.

我正在尝试通过第 1 页中的以下代码创建 SESSION 变量:

I am trying to create SESSION variables through the following code in page 1:

<?php
  $conditionArray = array('Past', 'Past', 'Future', 'Future');
  $typeArray = array('Gains', 'Gains', 'Losses', 'Losses');
  shuffle($conditionArray);
  shuffle($typeArray);
  session_start();
  $_SESSION['conditionArray'] = implode(',',$conditionArray);
  $_SESSION['typeArray'] = implode(',',$typeArray);
  var_dump($_SESSION);
?>
<html>
<body>
</body>
</html>

现在 var_dump 显示以下内容:

Now var_dump shows the following:

array(2) { ["conditionArray"]=> string(23) "Future,Past,Past,Future" ["typeArray"]=> string(25) "Gains,Gains,Losses,Losses" }

这很好.

在下一页上,我尝试通过以下代码检索变量:

On the next page I am trying to retrieve a variable by the following code:

<?php
  session_start();
  echo $_SESSION['typeArray'];
  var_dump($_SESSION);
?>
<html>
<body>
</body>
</html>

它给了我:

array(0) { }

我尝试查看 php 的错误日志文件,但没有看到任何相关内容.任何帮助将不胜感激!

I tried looking into the error log files of php and I don't see anything relevant. Any help would be greatly appreciated!

已修复:

找到解决方案后我觉得很愚蠢,但@webgal 是对的.我需要将 session_start() 放在 < 之前!DOCTYPE HTML> 也是如此.我把它放在它的正下方和 html 标签上方.我要感谢大家的时间、耐心和努力,并真诚地为我的无知道歉!

I feel rather stupid after finding the solution but @webgal was right. I needed to put session_start() before < !DOCTYPE HTML> as well. I had it right below it and above the html tag. I'd like to thank you all for your time, patience and effort and sincerely apologize for my ignorance!