2

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(-1);


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.

3
  • Found this : php.net/manual/en/function.session-start.php#91759 Try with numeric keys. Nevertheless, as said in the doc : "A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL." Jun 30, 2010 at 14:54
  • I require a session to be independent from any other sessions that may exist because I am trying to implement a password login script to be included at the top of any arbitrary page. The password login script will prompt any user not logged in to enter a username and password. In the case of a successful login, the fact that a user has previously provided a correct username and password will be stored in a session. The script needs to work with any arbitrary page, so it cannot interfere with any session that may be used by the page for which access is restricted.
    – Tom
    Jun 30, 2010 at 15:02
  • The only problem you would have with using an existing session would be overwriting session variables of the same name. You could just prefix them with something unique, like $_SESSION['__login_successful'] = true;
    – Powerlord
    Jun 30, 2010 at 15:24

3 Answers 3

3

The session ID is read from a cookie when you run the first session_start() and sticks around even if you use session_write_close() (otherwise you wouldn't be able to call session_start() to reopen the same session)

I don't have PHP handy here to test this, but in theory, you can do session_id($_COOKIE['session_two']); before the second session_start() to switch to the correct ID.

I may have the cookie name wrong, though, so you may want to printr($_COOKIE); to see which cookies are set.

Edit: I forgot to mention: The session's cookie name is based on session_name, or at least it is if you only use a single session in a file.

1
  • Thank you very much. That fixed it, and your memory with regard to the cookie name was correct also.
    – Tom
    Jun 30, 2010 at 15:30
1

According to the docs,

As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

My interpretation is that no, you cannot use session_start() more than once per page, regardless of the name.

The regenerate_id() is changing the ID for the first session, and the second session_start is being ignored. That is why you see the behavior described.

I'd suggest letting us know what you're seeking to accomplish by doing this, and probably someone can recommend another way to do it.

2
  • No warning or error was generated by the above script because the initially started session was closed by the call to session_write_close(). Once the initially started session was closed, the script should have been in the same state as before any session was started, if session_write_close() indeed closes the session. I'm still confused.
    – Tom
    Jun 30, 2010 at 14:55
  • Hmm, I hadn't observed that you closed the first one, actually. The cookie issue sounds like a good explanation for this behavior. Glad you got it worked out!
    – JAL
    Jun 30, 2010 at 18:07
1

My solution is

ob_start();
$name1 = 'session_1';
session_name($name1);
if(empty($_COOKIE[$name1]))
    $sid = md5(microtime(true));
else
    $sid = $_COOKIE[$name1];
session_id($sid);
session_start();
$_SESSION['count']++;
print_r($_SESSION);
session_write_close();
$_SESSION = array();

$name2 = 'session_2';
session_name($name2);
if(empty($_COOKIE[$name2]))
    $sid2 = md5(microtime(true));
else
    $sid2 = $_COOKIE[$name2];
session_id($sid2);
session_start();
$_SESSION['count'] = $_SESSION['count'] + 2;
print_r($_SESSION);
session_write_close();
ob_end_flush();
1
  • :Please be more descriptive in your answer. Refer:How to Answer
    – askmish
    Oct 21, 2012 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.