3

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!

hrshd
  • 941
  • 2
  • 13
  • 19
  • 1
    Code works absolutely fine on my system.I am damn sure, there is something wrong with the xampp/wampp configuration. – Ritesh Kumar Gupta May 07 '13 at 15:30
  • @webgal it doesn't need to be the first function call, but it needs to be called before any output to browser occurs. – bitWorking May 07 '13 at 15:31
  • @webgal session_start() is on the first line on both pages – hrshd May 07 '13 at 15:36
  • @ritesh_nitw I am using MAMP. I'm not quite sure what should I be looking for? – hrshd May 07 '13 at 15:37
  • Is your `PHPSESSID` cookie variable the same between pageviews? If so, out of curiosity, what happens if you add `session_write_close();` after you set values in `$_SESSION` and then try to view the next page. Also, a silly question: the pages are being served from the same webserver and same domain, right? – WWW May 07 '13 at 15:44
  • @Crontab how do I check the PHPSESSID? I tried session_write_close(); and did'nt change anything.Both pages are being served by localhost on my machine. – hrshd May 07 '13 at 15:51
  • Check the cookie in your browser using something like Firebug or Chrome's developer tools. – WWW May 07 '13 at 16:46
  • If you check the comments below, I've mentioned that the session Id changes every time I refresh page1. Also when page2 loads, a new session Id is created. It should not happen that way right? – hrshd May 07 '13 at 16:51
  • 1
    I ran the same code on my production server and it worked perfectly! @ritesh_nitw was right. I need to check my MAMP configuration – hrshd May 07 '13 at 17:47
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – PeeHaa May 07 '13 at 18:08
  • you should include seesion_start(); at the top of the first page – Mashhood Ali Jul 28 '15 at 11:22

2 Answers2

5

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!

hrshd
  • 941
  • 2
  • 13
  • 19
  • In my case, I renamed the files, rewrote the code with different logic, restarted the Apache server, restarted the machine, and what not. Strangely, removing comments before "session_start()" did the trick. Can't help laughing. You reply gave me a shady hint. Thanks. – Kumar Kush Sep 29 '16 at 13:45
4

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.

raidenace
  • 12,789
  • 1
  • 32
  • 35
  • I am using MAMP and the sessions are being stored in the Applications/MAMP/tmp/php. So I believe permissions to that folder should be fine since it is within MAMP itself. I also see the session files stored in there. I am clueless what to do next! – hrshd May 07 '13 at 15:42
  • print session_id() in the next page and check what it returns – raidenace May 07 '13 at 15:46
  • print session_id() returns the filename of the cookie stored in the tmp/php file. But the id's are different for both pages. Is that normal? Also the session file created on loading page 2 is blank. – hrshd May 07 '13 at 15:58
  • session_ids should remain the same unless you used a session_regenerate_id()...Maybe your session cookie is dying when the page is exited. – raidenace May 07 '13 at 16:25
  • All the code I have used pertaining to sessions is in the code mentioned above. I am not using session_regenerate_id(). My seesion id changes even when I refresh. So a new file is created with the same content. That should'nt be the case should it? – hrshd May 07 '13 at 16:29