note 85433 added to function.session-regenerate-id
| From: | soapergem at gmail dot com | Date: | Fri, 29 Aug 2008 22:50:05 +0000 |
| Subject: | note 85433 added to function.session-regenerate-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143991@lists.php.net to get a copy of this message | ||
This can be a very dangerous function if you're not careful about how you handle things,
because even though it generates a whole new set of session data, it keeps the old data
"open" until the script terminates, locking out any other scripts trying to run
concurrently with the old session id.
Recently I came across a situation where I wanted to explicitly pass in a session ID, copy the data
from that session into a *new* session, and then continue operating under that new session, thereby
allowing other scripts to use the old one concurrently. But I quickly found that these "other
scripts" would not execute until the first script finished--even though it had already started
a new session--because it kept the old session open.
So if you're trying to copy over session data to a new session to free up the old session for
continued, concurrent use, here's some code to ensure nobody's feet get stepped on:
<?php
// get session id of an existing session
$sid = $_GET['sid'];
// start the old session to retrieve $_SESSION data
session_id($sid);
session_start();
// start a new session; this copies the $_SESSION data over
session_regenerate_id();
// hang on to the new session id
$sid = session_id();
// close the old and new sessions
session_write_close();
// re-open the new session
session_id($sid);
session_start();
/* main code here */
?>
This could probably be encapsulated into a function with one parameter as well to save space if it
was a repeated thing.
----
Server IP: 64.71.164.2
Probable Submitter: 66.170.10.66
----
Manual Page -- http://www.php.net/manual/en/function.session-regenerate-id.php
Edit -- https://master.php.net/note/edit/85433
Del: integrated -- https://master.php.net/note/delete/85433/integrated
Del: useless -- https://master.php.net/note/delete/85433/useless
Del: bad code -- https://master.php.net/note/delete/85433/bad+code
Del: spam -- https://master.php.net/note/delete/85433/spam
Del: non-english -- https://master.php.net/note/delete/85433/non-english
Del: in docs -- https://master.php.net/note/delete/85433/in+docs
Del: other reasons-- https://master.php.net/note/delete/85433
Reject -- https://master.php.net/note/reject/85433
Search -- https://master.php.net/manage/user-notes.php