I want to use two independent $_SESSIONs in a single PHP script.
I have attempted to verify that this is possible using the following code.
error_reporting(E_ALL ^ E_STRICT);
session_name('session_one');
session_start();
$_SESSION = array();
$_SESSION['session_one_var'] = 'test_one';
$output1 =
(
"session_id(): '" . session_id() . "'.<br/>\n" .
"session_name(): '" . session_name() . "'.<br/>\n" .
print_r($_SESSION, true)
);
session_write_close();
$_SESSION = array();
session_name('session_two');
session_start();
$_SESSION['session_two_var'] = 'test_two';
$output2 =
(
"session_id(): '" . session_id() . "'.<br/>\n" .
"session_name(): '" . session_name() . "'.<br/>\n" .
print_r($_SESSION, true)
);
session_write_close();
$_SESSION = array();
echo "$output1<br/>\n<br/>\n$output2";
Output:
session_id(): 'f19aecd8d3ce0c5d444456d2387c6e35'.
session_name(): 'session_one'.
Array ( [session_one_var] => test_one )
session_id(): 'f19aecd8d3ce0c5d444456d2387c6e35'.
session_name(): 'session_two'.
Array ( [session_one_var] => test_one [session_two_var] => test_two )
I expect the output to show that the session named 'session_one' contains array('session_one_var' => 'test_one') and the session named 'session_two' contains array('session_two_var' => 'test_two'). Instead, 'session_two' seems to be the same session as 'session_one'.
If I insert a line 'session_regenerate_id();' after the second 'session_start();' line, a different session id is reported for 'session_two', but the output is otherwise the same. See below.
session_id(): 'f19aecd8d3ce0c5d444456d2387c6e35'.
session_name(): 'session_one'.
Array ( [session_one_var] => test_one )
session_id(): '3bcc74a7bcbac30e680c5b94fadcede1'.
session_name(): 'session_two'.
Array ( [session_one_var] => test_one [session_two_var] => test_two )
What am I doing wrong?
I know similar questions have been asked on this forum before, here and here, but the answers offered so far have failed to enlighten me.
Any help will be much appreciated.