I would like to know why session_name()
seems to be working only one single time?
My situation where I use session_name()
is that I have a PHP script which receives a HTTP request with 2 cookies:
Cookie: PHPSESSNAME1=sessionid1; PHPSESSNAME2=sessionid2
I use PHP's session_name()
function to indicate the name of the session I would like to start (e.g. either PHPSESSNAME1
or PHPSESSNAME2
), thus expecting that this information is used to indicate the respective cookie to be used to determine the session_id.
This works well in both cases, for case PHPSESSNAME1
:
session_name('PHPSESSNAME1');
session_start();
echo session_id();
session_write_close();
// outputs -> 'sessionid1'
and equaly for PHPSESSNAME2
session_name('PHPSESSNAME2');
session_start();
echo session_id();
session_write_close();
// outputs -> 'sessionid2'
where each time the session_name()
call resulted into the respective session_id having been read. Used only one single time the session_name()
function as expected.
However, it does not work together
//(1) start session named PHPSESSNAME1
session_name('PHPSESSNAME1');
session_start();
echo session_id();
session_write_close();
echo ' , ';
//(2) continue with PHPSESSNAME2
session_name('PHPSESSNAME2');
session_start();
echo session_id();
session_write_close();
// outputs -> 'sessionid1 , sessionid1'
The result shows me that after the first session named PHPSESSNAME1
, was used, another call to session_name('PHPSESSNAME2');
did not - as I thought - make the "PHP session magic" update the session id to the correct value of sessionid2
.
Reading the documentation:
The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request (and before session_start() or session_register() are called).
makes me think, that session_start
is designed with the shortcoming of only working once, so that I would have to manually patch the updating of the session_id for the second session PHPSESSNAME2
, e.g somthing like this
session_name('PHPSESSNAME2');
// manually update the session_id (as session_name only works once)
session_id(isset($_COOKIE['PHPSESSNAME2'])?$_COOKIE['PHPSESSNAME2']: null);
session_start();
Hence my question: Is there a good reason, why session_name only works a single time, or is this merely a deficiency of PHP session handling?