note 74291 rejected from ref.session by james
| From: | james@php.net | Date: | Mon, 22 Oct 2007 20:06:15 +0000 |
| Subject: | note 74291 rejected from ref.session by james | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131640@lists.php.net to get a copy of this message | ||
Note Submitter: Marcin Wiazowski
----
In an example below, after session restart all session data will be lost without a workaround. It
seems to be a PHP problem with a local variable $_SESSION_copy reference counter (fixed in PHP 5.2.1
- I think it has been fixed unintentionally during fixing a #40274 bug).
<?php // test.php
$_SESSION_copy = '';
function test_restart_session()
{
// global $_SESSION_copy; // Workaround 1: make $_SESSION_copy variable global (living after
function return)
// static $_SESSION_copy; // Workaround 2: make $_SESSION_copy variable static (living after
function return)
session_destroy();
$_SESSION_copy = $_SESSION;
session_start(); // This function reads $_SESSION from session file (here: $_SESSION is emptied)
$_SESSION = $_SESSION_copy;
// $_SESSION['workaround'] = 1; // Workaround 3: set/change any $_SESSION key INSIDE
THIS FUNCTION
}
if (!isset($_GET['show_session_state']))
{
session_start();
test_restart_session();
$_SESSION['string_to_print'] = 'OK';
echo 'Here $_SESSION always contains valid data:
'.$_SESSION['string_to_print'];
echo '<br><br><a href="test.php?show_session_state=1">show
session data during next script execution</a>';
// Without workaround $_SESSION data will be emptied after script finishes!
}
else
{
session_start();
echo '$_SESSION data during next script execution:
'.$_SESSION['string_to_print'];
echo '<br><br><a href="test.php">test once
again</a>';
}
exit;
?>