PHP Session variables not being passed to the next page

纵然是瞬间 提交于 2019-12-31 03:44:08

问题


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.

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>

Now var_dump shows the following:

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. Any help would be greatly appreciated!


回答1:


FIXED:

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!




回答2:


This could be a permissions issue. Check where you sessions are being written to (session.save_path in your ini file). Usually it is in the /tmp folder. But if it set to another folder and your web server does not have permissions to write into it, you can encounter this issue.

Additionally in the next page print session_id() to check if your session has started.



来源:https://stackoverflow.com/questions/16422816/php-session-variables-not-being-passed-to-the-next-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!