I have a non CMS page in which a session value is stored using PHP’s default session handler. ie:
session_start();
$_SESSION['MyVar'] = true;
On another page, which is part of the CMS, I need to test whether the variable is true or not. But, the CMS uses it’s own session handler, so when I try to read the variable, it’s undefined, because it’s looking for it in the CMS’s session storage system, not PHP’s default.
How can I switch over for a moment and read a value from the default session store, and then reinstate the CMS’s so that it functions correctly?
Something like:
$SessionHandler = session_get_current_handler(); // save the CMS session handler
session_use_default(); // Use PHP's default handler
$Value = $_SESSION['MyVar']; // Get the value I need
session_set_handler($SessionHandler); // Restore CMS handler
At some point you will need to call
session_start()in order to populate the$_SESSIONsuperglobal array. You ‘might’ be able to callsession_write_close(), change the handler, and dosession_start()again.Try it, it will look something like
I almost doubt it is going to work though. The only reason it ‘might’ work is because of the
session_write_close()as PHP normally throws anE_NOTICEif you try to callsession_start()twice.Depending upon the implementation, it might try to get the data again. But a lot of people over at the documentation comments have seen weird things happen when calling
session_start()twice, even aftersession_write_close()