11

Baffling!
I have a page that should have some sessions stored, yet when I try to print them nothing shows up.
There must be sessions there because this first page has a link that takes me to a second page and in that second page I can successfully display the sessions!

<? 
  session_start(); 
  include "frontend/header.php";
?>
</head>

<body>
  <div id="header">
        <? include "frontend/menu.php";?>
        <div class="clear"></div>
  </div>
  <div id="green_bg" style="display:block">

<?
  echo "1";
  echo "<pre>";print_r($_SESSION); echo"</pre>";
?>

<div class="main thank-you">
<a class="thank_you">Thank You</a>
<p>Your phone number will be refilled in a moment. <br/>
  Please check your emails for confirmation.
</p>

<div class="signUp4MonthlyRefills" style="background-color: #f7931e; padding: 20px;">

  <h3>Do you want to save time and effort?</h3>
  <br/>
  Then leave the refills to us!! 
  <br/>
  <br/><i>Introducing</i>... Auto Monthly Refills!!
  <p>Sign up for automatic monthly refills and you'll never have to worry about your phone running out of credit! 

    <h1 align="center"><a href="auto-monthly-refills-form.php" style="font-size: 30px">Click Here To Sign Up.</a></h1></p>
   </div>
<br />

</div>
</div>
<? include "frontend/footer.php";?>

When the code calls for the sessions, the page just displays '1' and then an empty array (i.e. it prints "array()" ).

Then when clicking on the link to "auto-monthly-refills-form.php" it displays all the sessions! The "auto-monthly-refills-form.php" file looks simply like this:

<?
  session_start();
  echo "<pre>";print_r($_SESSION); die();

And the sessions are now displayed (a lot of data)!!
If anyone has any ideas what might be the issue...

UPDATE:
Even after deleting the session_start() on both pages it still works as normal! How could this be?

Ben
  • 515
  • 5
  • 18
  • And what value it shown on your second page? – Alive to die - Anant May 01 '16 at 13:11
  • Any change if you remove the session_start(); from the main page?Since it exist in the included page. – Mihai May 01 '16 at 13:11
  • @Anant lots of data (many different variables) stored in the session... – Ben May 01 '16 at 13:13
  • @Mihai Everything is the same even when removing session_start(); – Ben May 01 '16 at 13:19
  • Your die might interfere with the rest of the script,remove it. – Mihai May 01 '16 at 14:26
  • 2
    wierd... try the print_r right after session_start() on the first page. Also, what happens if you refresh the page? Could it be caching? Also, how do you jump to the first page? if you link from the second page to the first, does it still do it? – Christian Cerri May 04 '16 at 10:08
  • 2
    debugging: I would: 1) set error reporting on: [Display PHP Errors](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). 2) `var_dump($_COOKIE, $_SESSION);` immediately after session start in both scripts. Why? You should see the session cookie and any session data. 3) To ensure the session isn't active, before the session_start then add `var_dump(isset($_SESSION));`. It should report `false` in both cases. This is likely to provide enough clues to determine what is happening. – Ryan Vincent May 04 '16 at 10:10
  • Do you have any session related code in frontend/header and fromtend/menu php files? Maybe you are closing sessions in those files? Try to move session print_r to the top just after the line where you are starting the session and see if it has an output. – Nick Surmanidze May 04 '16 at 10:17
  • @RyanVincent thanks. Check out my update (even when erasing the session_start() from both pages it still is working fine! That prob makes sense with 'var_dump(isset($_SESSION))' actually returning 'true' for me! What now? How is it still working without the session_start()?? – Ben May 04 '16 at 14:51
  • 1
    So, assuming your files are called `test_file1.php` and `test_file2.php2`. Then in your web-browser - you enter: `http://localhost/test_file1.php` and it reports that the `var_dump(isset($_SESSION));` before the `session_start();` instruction returns true? That doesn't sound right. You are not running these files in a 'framework' are you? Please add the version of PHP / O/S and webserver you are using to your question. – Ryan Vincent May 04 '16 at 15:05
  • 1
    have you addressed the cache concern brought up by @ChristianCerri? try pressing `shift + f5` to fully reload the page. – Jeff Puckett May 04 '16 at 15:22
  • @JeffPuckettII I tried 'shift + f5'... nothing doing. The $_SESSION is simply empty in the first file and full in the second... I think the answer lies in the clue about the sessions still working even without 'session_start'... Just not sure what to make of that... – Ben May 04 '16 at 17:46
  • @RyanVincent No, I'm not using a framework... – Ben May 04 '16 at 17:49
  • There are no other `session_start();` instructions anywhere in the included files? `grep`? – Ryan Vincent May 04 '16 at 18:11
  • @RyanVincent Nope... anyway i am printing the '$_SESSION' before any files are included... (and it still works!!!?!) – Ben May 04 '16 at 18:27

1 Answers1

2

Okay... Sorted it out... Please read...

Even though this is a new page, the code is sort of coming off layers of other files...

I didn't think this should have been an issue...

(I had thought that each page needed it's own session_start() but for some reason they don't and all of them apparently are based off one page... bit complicated to explain - suffice it to say that I am trying to sort out someone else's code :-P)

Anyway, I went through all the files that are connected to this one, deleted all the session_start()s from them besides for the primary one. Now it's working fine.

The data now displays perfectly!

Question:
Does anyone have an explanation for this?
Why does having too many session_starts() cause problems?

Ben
  • 515
  • 5
  • 18
  • 1
    Technically, multiple session_start()s should work, albeit slow. E_NOTICE is generated, maybe this screwed it up. Anyway, best to only call it once, or check if $_SESSION is set before calling it. Glad you got it working.... – Christian Cerri May 04 '16 at 22:19
  • @ChristianCerri Thanks for the help :-). Strange thought that it was screwing up in the first file and not in the second... If anything, it should have been the other way round...? – Ben May 04 '16 at 22:23