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