2

The following script creates two cookies (SESSION1 and SESSION2), however, both contain the same session_id value.

How can I modify this script so that both sessions will be independent?

Thank you

<?php
$t=time();
session_name('SESSION1');
session_start();
$_SESSION['s1_'.$t]=$t;
echo('SESSION1<pre>'.print_r($_SESSION,1).'</pre>');
session_write_close();
$old_session=session_name('SESSION2');
session_start();
$_SESSION['s2_'.(2*$t)]=2*$t;
echo('SESSION2<pre>'.print_r($_SESSION,1).'</pre>');
session_write_close();
session_name($old_session);
session_start();
echo('SESSION1<pre>'.print_r($_SESSION,1).'</pre>');
?>

1 Answer 1

1

You also need to change session ID for each new session. Try this:

$t=time();

session_name('SESSION1');
$s1 = session_id('ID1');
session_start();
$_SESSION['s1_'.$t]=$t;
echo('SESSION1<pre>'.print_r($_SESSION,1).'</pre>');
session_write_close();

$old_session = session_name('SESSION2');
$s2 = session_id('ID2');
session_start();
$_SESSION['s2_'.(2*$t)]=2*$t;
echo('SESSION2<pre>'.print_r($_SESSION,1).'</pre>');
session_write_close();

session_name($old_session);
session_id('ID1');
session_start();
echo('SESSION1<pre>'.print_r($_SESSION,1).'</pre>');
Sign up to request clarification or add additional context in comments.

1 Comment

My only problem is I do not wish to specify a session ID, but use system generated ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.