Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:38:56+00:00 2026-05-15T12:38:56+00:00

I want to use two independent $_SESSIONs in a single PHP script. I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:38:57+00:00Added an answer on May 15, 2026 at 12:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use two meta:resourcekey in single control is it possible? for example
Scenario I have a windows forms application. I want to use two different WCF
I want to use an AOP framework, but I have two constraints. Any framework
i have two text inputs like the following, i don't want to use <form>
I have a desktop C# app that I want to split into two parts
Want to use two models in one view. I have two controllers one for
I want to use these two classes from lucene - import org.apache.lucene.analysis.snowball.*; import org.apache.lucene.analysis.PorterStemmer;
When we want to modify some value in one object we may use two
I want to use a configuration file to create two loggers which will log
I want to use rsync to synchronize two directories in both directions. I refer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.