/    Sign up×
Community /Pin to ProfileBookmark

Why Php Assumes No Session Started When It Has Been Started ?

Php Folks,

Check this out.

Why on 2nd page load, the ELSEIF does not trigger ?

My Expectation:
On the 1st page load, the IF should trigger since there are no sessions at the beginning.
But on the 2nd page load, the ELSEIF should trigger since a session has been started previously and form step had been set to ‘end’.

[code]
if(!session_id() || $_SESSION[‘form_step’] != ‘end’)
{
session_start();
$_SESSION[‘form_step’] = ‘start’;
echo “Line: 28 “; echo “Session Step: “; echo $_SESSION[‘form_step’]; echo “<br>”;
echo “Line: 29 “; echo “New Session Id: “; echo session_id(); echo “<br>”;
$_SESSION[‘form_step’] = ‘end’;
}
elseif($_SESSION[‘form_step’] == ‘end’) //Q2. WHY THIS IF GETS TRIGGERED WHEN CLICKING ANY NUMBERED PAGE LINKS (ON PAGINATION SECTION (EG PAGE 1 2 3 ETC.)) SINCE SESSION ID ALREADY EXISTS DUE TO [‘form_step’] = ‘end’ ?
{
//session_start();
echo “Line: 35 “; echo “Session Step: “; echo $_SESSION[‘form_step’]; echo “<br>”;
echo “Line: 36 “; echo “New Session Id: “; echo session_id(); echo “<br>”;
}
[/code]

Guess what ? On 2nd page load or same page reload, even though a session exists, the ELSEIF doesn’t trigger but the IF triggers instead as if there are no sessions in existance or no sessions had been started yet.
Why is that ? Test the code on your own end and see for yourself. On the 1st page load, the IF will trigger. But refreshing the page would result in the same IF getting triggered again instead of the ELSEIF!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorJun 21.2020 — Here's another test:

<i>
</i>if(!session_id() || session_id() != $session_id || $_SESSION['form_step'] != 'end')
{
session_start();
$_SESSION['form_step'] = 'start';
echo "Line: 28 "; echo "Session Step: "; echo $_SESSION['form_step']; echo "&lt;br&gt;";
echo "Line: 29 "; echo "New Session Id: "; echo session_id(); echo "&lt;br&gt;";
$_SESSION['form_step'] = 'end';
$session_id = session_id();
}
elseif($_SESSION['form_step'] == 'end') //Q2. WHY THIS IF GETS TRIGGERED WHEN CLICKING ANY NUMBERED PAGE LINKS (ON PAGINATION SECTION (EG PAGE 1 2 3 ETC.)) SINCE SESSION ID ALREADY EXISTS DUE TO ['form_step'] = 'end' ?
{
//session_start();
echo "Line: 35 "; echo "Session Step: "; echo $_SESSION['form_step']; echo "&lt;br&gt;";
echo "Line: 36 "; echo "New Session Id: "; echo session_id(); echo "&lt;br&gt;";
}


On the very 1st page load, obviously the IF will get triggered since there are no session_id() due to no sessions started yet.

Then a session will start.

$_SESSION['form_step'] = 'start';

Finally, session will be set to:

$_
SESSION['form_step'] = 'end';

Now, when I reload the page, the IF should not get triggered but it does. It should not trigger due to these reasons:

  • 1. Session Exists.

  • 2. session_id() == $session_id.

  • 3. $_SESSION['form_step'] == 'end'.


  • And so, when I reload the page, the ELSEIF should have triggered. But it doesn't!

    Why is that ?
    Copy linkTweet thisAlerts:
    @NogDogJun 21.2020 — Since you have not yet done session_start(), the first part of your if() condition will always be true (session_id() will be false -- due to it being an empty string); so you will always execute that first if condition. Since everything else is OR'ed, it doesn't matter whether anything else in the if() condition is true or not.

    Instead, do session_start() at the very beginning, then test if something is set in $_SESSION to determine if the user has an active session.
    ×

    Success!

    Help @developer_web spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...