note 74291 added to ref.session

From: Date: Tue, 03 Apr 2007 20:28:28 +0000
Subject: note 74291 added to ref.session
Groups: php.notes 
Request: Send a blank email to php-notes+get-125061@lists.php.net to get a copy of this message
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; ?> ---- Server IP: 83.64.128.68 Probable Submitter: 83.7.237.119 ---- Manual Page -- http://www.php.net/manual/en/ref.session.php Edit -- https://master.php.net/note/edit/74291 Del: integrated -- https://master.php.net/note/delete/74291/integrated Del: useless -- https://master.php.net/note/delete/74291/useless Del: bad code -- https://master.php.net/note/delete/74291/bad+code Del: spam -- https://master.php.net/note/delete/74291/spam Del: non-english -- https://master.php.net/note/delete/74291/non-english Del: in docs -- https://master.php.net/note/delete/74291/in+docs Del: other reasons-- https://master.php.net/note/delete/74291 Reject -- https://master.php.net/note/reject/74291 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125061) next »