Php Experts,
Why the session ends on every page load ? And when a new session gets started on every page load then why the session id does not change into a new one ? Always the same session id().
Looking at my code, you will see it clearly states that when the page loads, if it finds any existing session then they should be killed. And then echo "Line 11" & "line 12" as a confirmation that all the session have been killed.
Then start a new session. And echo "Line 18" and "line 19" as a confirmation that a new session has been started. That is what I intended.
You may wonder, why the session_start() is not at the top of the page but instead there is the condition to check if any sessions exist or not. That is because, this is a one page script and a session starts showing a webform when the page is loaded for the 1st time on your screen. When you press the SUBMIT button, the same page loads again (2nd round) with different $_GETs params on the url and the appropriate functions() trigger based on the $GETs params. On this 2nd round, the CONDITION should check whether any sessions exist or not and kill them all, if any exist. Hence, the condition exists at the top:
if(isset($_SESSION['form_step']))
{
unset( $_SESSION['form_step'] );
session_unset();
session_destroy();
Code
<?php
error_reporting(E_ALL);
if(isset($_SESSION['form_step']))
{
unset( $_SESSION['form_step'] );
session_unset();
session_destroy();
echo "Line: 11 "; echo "session destroyed!<br>";
echo "Line: 12 "; echo "Session Step: "; echo $_SESSION['form_step'];
}
if(!session_id())
{
session_start();
$_SESSION['form_step'] = 'start';
echo "Line: 18 "; echo "Session Step: "; echo $_SESSION['form_step']; echo "<br>";
echo "Line: 19 "; echo "New Session Id: "; echo session_id(); echo "<br>";
}
TEST RESULT: I load the page for the very 1st time. Obviously there will be no sessions at the beginning. And so, I do not get echoed "Line 11" & "line 12". The, session starts. I get echoed "Line 18" & "line 19" (as I should). So far, so good.
Bear in mind that, a session has now initiated on the 1st page load. I then reload the page by using CTRL+R or by pressing the enter button on the url field. Result ? When I reload the page for the 2nd time, the script should have found a session existing (since we started one on the 1st round). Therefore, the 1st "IF" condition should have got triggered here on the 2nd round, echoing "Line 11" & echoing "line 12". But they fail to get echoed. Q1. Why is that ?
This echoing failure only means one thing. That, when I load the same page for the 2nd time (running on the 2nd round), the php found no session. Even though a session exists. Why did it not find the session that was started on the 1st round (the one that started when I loaded the page for the 1st time) ? It must mean the session got killed somewhere along the line. if so, then ...
Q2. Where did the session get killed ? On every page load ? If so, then what is killing the session on every page load ? If not, then when did the session die before the page got reload ?
Q3. if the session got killed before the page reload then on the 2nd page load, why is there no new session_id() ? Why the old one displays ?
I need the answers to these 3 questions to solve this issue. Been going round in circles for 2 nights now, unable to get over the puzzle. Confused very badly. Seriously.