note 96309 added to ref.session
| From: | JeremySpeer at osu1 dot php dot net | Date: | Fri, 19 Feb 2010 20:14:03 +0000 |
| Subject: | note 96309 added to ref.session | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166194@lists.php.net to get a copy of this message | ||
When working on a project, I found a need to switch live sessions between two different pieces of
software. The documentation to do this is scattered all around different sites, especially in
comments sections rather than examples. One difficulty I encountered was the session save handler
for one of the applications was set, whereas the other was not. Now, I didn't code in the
function session_set_save_handler(), instead I utilize that once I'm done with the function
(manually), however this function could easily be extended to include that functionality. Basically,
it is only overriding the system's default session save handler. To overcome this after you
have used getSessionData(), just call session_write_close(), session_set_save_handler() with the
appropriate values, then re-run session_name(), session_id() and session_start() with their
appropriate values. If you don't know the session id, it's the string located in
$_COOKIE[session_name], or $_REQUEST[session_name] if you are using trans_sid. [note: use caution
with trusting data from $_REQUEST, if at all possible, use $_GET or $_POST instead depending on the
page].
<?php
function getSessionData ($session_name = 'PHPSESSID', $session_save_handler =
'files') {
$session_data = array();
# did we get told what the old session id was? we can't continue it without that info
if (array_key_exists($session_name, $_COOKIE)) {
# save current session id
$session_id = $_COOKIE[$session_name];
$old_session_id = session_id();
# write and close current session
session_write_close();
# grab old save handler, and switch to files
$old_session_save_handler = ini_get('session.save_handler');
ini_set('session.save_handler', $session_save_handler);
# now we can switch the session over, capturing the old session name
$old_session_name = session_name($session_name);
session_id($session_id);
session_start();
# get the desired session data
$session_data = $_SESSION;
# close this session, switch back to the original handler, then restart the old session
session_write_close();
ini_set('session.save_handler', $old_session_save_handler);
session_name($old_session_name);
session_id($old_session_id);
session_start();
}
# now return the data we just retrieved
return $session_data;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 24.86.92.63
----
Manual Page -- http://www.php.net/manual/en/ref.session.php
Edit -- https://master.php.net/note/edit/96309
Del: integrated -- https://master.php.net/note/delete/96309/integrated
Del: useless -- https://master.php.net/note/delete/96309/useless
Del: bad code -- https://master.php.net/note/delete/96309/bad+code
Del: spam -- https://master.php.net/note/delete/96309/spam
Del: non-english -- https://master.php.net/note/delete/96309/non-english
Del: in docs -- https://master.php.net/note/delete/96309/in+docs
Del: other reasons-- https://master.php.net/note/delete/96309
Reject -- https://master.php.net/note/reject/96309
Search -- https://master.php.net/manage/user-notes.php