|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-07-14 10:23 UTC] m dot v dot veluw dot smscity at gmail dot com
Description:
------------
When using session_start() and session_write_close() with the same session id/name will add multiple session cookies with the exact same contents everytime session_start is used.
This is useless overhead if it is the same.
Reproduce code:
---------------
session_name('uniqueName1');
session_start();
$_SESSION['Foo1'] = 'Bar1';
session_write_close();
session_name('uniqueName2');
session_start();
$_SESSION['Foo2'] = 'Bar2';
session_write_close();
session_name('uniqueName1');
session_start();
$sessionValue = $_SESSION['Foo1'];
print $sessionValue;
session_write_close();
session_name('uniqueName2');
session_start();
$sessionValue = $_SESSION['Foo2'];
print $sessionValue;
session_write_close();
Expected result:
----------------
just 1 session cookie header for uniqueName1.
just 1 session cookie header for uniqueName2.
Actual result:
--------------
2 session cookie headers for uniqueName1, where both are exactly the same
2 session cookie headers for uniqueName2, where both are exactly the same
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 21 05:00:01 2025 UTC |
I just ran into this bug in PHP 5.3.5 when working with a script that does lots of session_start()/session_write_close() in a long-running task, so that separate requests can still access the session during that long task. (Specifically those separate requests are checking the progress of the long task.) The resulting absurdly redundant Set-Cookie header caused Firefox 7 to lock up for a few seconds, and caused IE8 to give its infamously useless "Internet Explorer cannot display the webpage" page. So this bug is not "Bogus" s it claims. I do have a workaround, however. I'm already doing an ob_start() at the top of the script, and now before the ending ob_end_flush() I replace the Set-Cookie header with a new one: if (SID) header('Set-Cookie: '.SID.'; path=/', true); After adding this, I no longer have the above problems in Firefox and IE.I believe the problem is a missing PHP capability for session handling,without which no efficient solution is possible for this problem. In addition to session_start() and session_write_close(), PHP should have a session_write_reopen() function. This would solve several problems cleanly. It will allow for those that want fine-grained control over the transaction handling/demarcation when accessing session variables, without imposing any additional complications on those that just want the default session handling behavior. for example: at the top of all pages you start your session with: session_start(); session_write_close(); //no further blocking //.. rest of long running script execution //now we only block for tiny fraction of time while manipulating session vars startSessionTransaction(); $x = $_SESSION['x']; $x++; $_SESSION['x] = $x; endSessionTransaction(); //now we stop block //... script can continue running tedious operations without blocking others on session access //... and the user would then implement these function startSessionTransaction() { session_write_reopen(); } function endSessionTransaction() { session_write_close(); } Now you can only let your session handling part of your script block for the tiny parts when a session variable is manipulated, without having to completely restart sessions, because restarting sessions later in your script creates several additional problems as noted - such as creating duplicate session cookies, and just as annoying, force you to turn on output buffering for your entire script, since you cannot start (or restart) session's once any output has been sent to the browser. This is the solution required. This is what is missing in PHP session functionality. IMNSHOI also just ran into this issue on PHP 5.6.30-7+deb.sury.org so if it was fixed it would be nice to know which version it was fixed in. I have a long running download process kicked off from an Ajax request to PHP that updates state every few seconds to $_SESSION that calls session_start() before each update and session_write_close() after. Subsequent Ajax calls to check state are sent every few seconds by the browser. Longer requests would never seem to terminate and it was because hundreds or more Set-Cookie lines with the same session_id were being sent. The browser or Ajax stack couldn't handle all the header data (but no PHP or browser script errors). PHP source didn't yield any solutions but revealed a workaround. In the long running script, add this before the later session_start() calls: ini_set('session.use_cookies', 0); At the very least PHP sees this setting when calling session_start() and will not append additional session ID cookies to the response. This fixed the issue for me.