0

I am unable to destroy a specific session.

first session is started when logged in into website.

and the other is after the checkout page.

After the checkout page there is the order-status page.

And there I am using session_unset($_SESSION['SESSION____NAME']);

Help me I am Confused plz help me.....

2 Answers2

2

You have to work with a session_id instead of a session name. You can find the answer here: https://stackoverflow.com/a/24965106/9592932. Just add a https://www.php.net/manual/en/function.session-destroy.php to the correct session.

As you describe in the question you are working with two sessions: "first session is started when logged in into website. and the other is after the checkout page."

So if i populate two sessions with the same variable name value:

// populate 2 sessions with identical keys
for ($n = 1; $n != 3; $n++) {
    $id = 'session' . $n;
    session_id($id);
    session_start();
    $_SESSION["value"] = 'we are in: ' . $id;
    echo  $id . ": " . $_SESSION["value"]."\n";
    session_write_close();
}
print_r($_SESSION);

Produces:
  session1: we are in: session1
  session2: we are in: session2
  Array
  (
    [value] => we are in: session2
  )

If you unset($_SESSION['value']) you will only unset the 'value' from the second session and not the first session. To access the first session you have to:

    // switch back to first session
    // make sure previous session is closed
    session_id('session1'); 
    session_start();
    print_r($_SESSION); //  Array ( [value] => we are in: session1 )
    unset($_SESSION['value']); // to unset 'value'
    session_unset(); // to unset all values in session (does not take parameter)
    session_destroy(); // to destroy the first session
Ties Vandyke
  • 154
  • 1
  • 8
  • it's easy to use session name here no need to find ID and remove it $_SESSION is an Associative Array so using unset() function in php you can remove array item from its key(name). – S.Sachith Dec 20 '20 at 05:26
  • 1
    Again, this is just for a session value within one session, not a value in a specific session. – Ties Vandyke Dec 20 '20 at 06:11
  • can you tell me what is session do you know that is an associative array? – S.Sachith Dec 20 '20 at 06:12
  • "can you tell me what is session do you know that is an associative array? " -> I cannot answer a incomprehensible question – Ties Vandyke Dec 20 '20 at 06:20
  • You are technically correct, but sometimes uneducated people call session variables just sessions so it could be the case. Nevertheless, that's a correct answer to the question asked – Your Common Sense Dec 20 '20 at 06:25
  • @YourCommonSense i dont know man can you tell me this if we can use two session veriable names in this case it easier to use unset() right? – S.Sachith Dec 20 '20 at 06:43
  • @S.Sachith a "session variable" and a "session" are two different matters. You are talking about session variables but the question is about entire sessions. It is possible to have several different sessions for the same user. Entire sessions, each with it's own variables. If you start one session, the $_SESSION array will have one contents. If you start another session it will be another. Hence you cannot just use unset. You have to start the correct session first. – Your Common Sense Dec 20 '20 at 06:49
  • @YourCommonSense oh now i get it thank you :) – S.Sachith Dec 20 '20 at 06:58
-1

you have use unset() function to unset specific session variable here is the code

 unset($_SESSION['SESSION____NAME']);

learn more about php session handling from here

S.Sachith
  • 536
  • 1
  • 9
  • 21