As a bit of background, with the particular PHP application in question, I've always called session_start() in a "global include", the contents of which are executed on every page of a the site. This made sense, because I was accessing session data on every single page.
I later came to realize the caveats of such an approach, namely, unbearable wait-times due to session file locking. So, I decided to restructure the approach to sessions and wrote a couple of simple functions:
class GlobalMethods
{
function sessionStart()
{
if (session_id() == '') {
session_start();
}
}
function sessionEnd()
{
if (session_id() != '') {
session_write_close();
}
}
}
I removed session_start() from the global include file and combed through the site, placing calls to my custom sessionStart() function just before access to $SESSION data was required, and calls to sessionEnd() once I had what I needed from the $SESSION superglobal.
I've run into several issues. First, the above functions do not behave as I had expected. Any data that I write to the session, e.g. with
$_SESSION['someVar'] = 'someValue';
is not retained across pages. However, if I remove the conditional statements, and do something like the following, the observed behavior is as expected.
function sessionStart()
{
@session_start();
}
function sessionEnd()
{
session_write_close();
}
So, I thought to myself, "Well, I don't really understand why the original functions didn't behave as expected, but this latter approach seems to work without issue. I'll research the 'why' later." I had tested several areas of my site and didn't see any problems relative to sessions.
Then, I came upon the situation that prompted me to post in frustration. I was storing some session data just before sending headers and exiting, and it couldn't be retrieved on the page to which I was redirecting, after calling session_start(). A bunch of other session information was present, but not the most recent information from the previous page in the application-flow. I added the following to the troublesome script, just before the calls to header() and die():
var_dump(session_id());
echo '<br /><br />BEFORE calling sessionEnd:<br /><br />';
var_dump($_SESSION);
GlobalMethods::sessionEnd();
echo '<br /><br />AFTER calling sessionEnd and then sessionStart:<br /><br />';
GlobalMethods::sessionStart();
var_dump($_SESSION);exit;
The above snippet yields the following:
string(26) "sttrjvufik60nmb653ro1ojet7"
BEFORE calling sessionEnd:
array(1) {
["checkout"]=>
array(4) {
["orderId"]=>
string(5) "19257"
["completedSections"]=>
array(2) {
[0]=>
string(13) "accountPrompt"
[1]=>
string(13) "paymentMethod"
}
["paymentMethod"]=>
array(2) {
["bindKey"]=>
string(40) "9f5711356955e4ff844766af6b70b6ee5b5aec7e"
["method"]=>
string(6) "credit"
}
}
}
AFTER calling sessionEnd and then sessionStart:
array(1) {
["checkout"]=>
array(2) {
["orderId"]=>
string(5) "19257"
["completedSections"]=>
array(1) {
[0]=>
string(13) "accountPrompt"
}
}
}
If I examine the actual session storage file, on the filesystem, the data is missing from there, too. The session file is modified each time I reload the page, but the information therein never changes.
Am I missing something terribly obvious? Any help would be sincerely appreciated.
Thanks in advance!