简体   繁体   English

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

[英]PHP Session variables not being passed to the next page

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. 我知道这个标题是与1000篇与某个问题相关的其他帖子的重复,如果解决方案存在,我事先表示歉意。 Trust me I went through tons of posts regarding this topic and tried all the solutions prescribed, but have had no luck. 相信我,我经历了有关该主题的大量帖子,并尝试了所有规定的解决方案,但是没有运气。

I am trying to create SESSION variables through the following code in page 1: 我正在尝试通过第1页中的以下代码创建SESSION变量:

<?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>

Now var_dump shows the following: 现在,var_dump显示以下内容:

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

which is fine. 很好

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>

and it gives me: 它给了我:

array(0) { }

I tried looking into the error log files of php and I don't see anything relevant. 我尝试查看php的错误日志文件,但没有发现任何相关信息。 Any help would be greatly appreciated! 任何帮助将不胜感激!

FIXED: 固定:

I feel rather stupid after finding the solution but @webgal was right. 找到解决方案后,我感到很愚蠢,但是@webgal是正确的。 I needed to put session_start() before < !DOCTYPE HTML> as well. 我还需要将session_start()放在<!DOCTYPE HTML>之前。 I had it right below it and above the html tag. 我在它的下方和html标签上方。 I'd like to thank you all for your time, patience and effort and sincerely apologize for my ignorance! 我要感谢大家的时间,耐心和努力,并为您的无知表示由衷的歉意!

This could be a permissions issue. 这可能是权限问题。 Check where you sessions are being written to (session.save_path in your ini file). 检查会话要写入的位置(ini文件中的session.save_path)。 Usually it is in the /tmp folder. 通常它在/ tmp文件夹中。 But if it set to another folder and your web server does not have permissions to write into it, you can encounter this issue. 但是,如果将其设置为另一个文件夹,并且您的Web服务器没有写入该文件夹的权限,则可能会遇到此问题。

Additionally in the next page print session_id() to check if your session has started. 另外,在下一页中,打印session_id()来检查会话是否已开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM