0

Folks,

Look at this code. It gives no error:

//WORKING FINE
session_start();
echo session_id();
echo "<br>";
echo __LINE__;
echo "<br>";

if(!session_id() || !isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end')
{
    echo __LINE__;
    echo "<br>";
    $_SESSION['form_step'] = 'start';
    echo $_SESSION['form_step'];
    echo "<br>";
    echo session_id();
    echo "<br>";
    $_SESSION['form_step'] = 'end';
    echo $_SESSION['form_step'];
    echo "<br>";
    echo __LINE__;
    echo "<br>";
}
elseif($_SESSION['form_step'] == 'end')
{
    echo __LINE__;
    echo "<br>";
    echo $_SESSION['form_step'];
    echo "<br>";
    echo session_id();
    echo "<br>";
    echo __LINE__;
    echo "<br>";
}

And look at this one that gives error: Notice: Undefined index: form_step in C:\xampp\htdocs\power.page\pagination_test_SIMPLE.php on line 143

//WHY GIVES UNDEFINED ERROR ON LINE: elseif($_SESSION['form_step'] == 'end')
session_start();
echo session_id();
echo "<br>";
echo __LINE__;
echo "<br>";

if(!session_id())
{
    if(!isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end')
    {
        echo __LINE__;
        echo "<br>";
        $_SESSION['form_step'] = 'start';
        echo $_SESSION['form_step'];
        echo "<br>";
        echo session_id();
        echo "<br>";
        $_SESSION['form_step'] = 'end';
        echo $_SESSION['form_step'];
        echo "<br>";
        echo __LINE__;
        echo "<br>";
    }
}
elseif($_SESSION['form_step'] == 'end')
{
    echo __LINE__;
    echo "<br>";
    echo $_SESSION['form_step'];
    echo "<br>";
    echo session_id();
    echo "<br>";
    echo __LINE__;
    echo "<br>";
}
die;

Both codes are practically the same. I just changed this line from the 1st code:

if(!session_id() || !isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end')
{

to this line in the 2nd code:

if(!session_id())
{
    if(!isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end')
    {

That is all. The 2nd code giving the error. Q1. How come the 1st code is not giving that same error ?

1 Answers1

0

You get the error because in the second code you only test

if(!session_id())

and then in the else, which you go to if there is a session started

elseif($_SESSION['form_step'] == 'end')

you have not actually checked that the specific $_SESSION['form_step'] actually exists, you have just assumed it will, and obviously in some case(s), it will not yet exist.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I understand now that on the 1st code, the: if(!session_id() is not triggering the IF but the: !isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end')' is triggering it and during the trigger, the: $_SESSION['form_step'] is getting defined. Hence, when I refresh the page, on the 2nd round the ELSEIF is getting triggered and it finds the: $_SESSION['form_step'] defined. Hence, shows no problem. While with the 2nd code, unlike the 1st code, the ELSEIF is getting triggered first and it finds the: $_SESSION['form_step'] not defined yet and so gives the error. Thanks! – Tiger Coder Jun 24 '20 at 12:38
  • You see, I thought on the 1st code, the: if(!session_id() was triggering the IF. But it actually wasn't. That wrong thinking of mine was taking me off track to confusion! – Tiger Coder Jun 24 '20 at 12:45