note 109981 deleted from session.examples.basic by cmb

From: Date: Wed, 07 Dec 2016 16:01:25 +0000
Subject: note 109981 deleted from session.examples.basic by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208265@lists.php.net to get a copy of this message
Note Submitter: jpleveille at webgraphe dot com ---- As mentioned in the documentation, using session_write_close() shuts down the session. It is particularly useful if you want to use header('Location: SOMEURL'); to a URL within the same scope of the current script, that will load the session. Why? When you use this header directive, the browser is requested to redirect the user to the given URL. If that URL is in the scope of the script where header() is called (let's say, same URL), the requested URL "COULD" load the session BEFORE it has actually been shut down in the previous script, and you might end up with the session from the previous request, reverting all modifications to session in the the last script. <?php session_start(); if (!isset($_SESSION['hello'])) { $_SESSION['hello'] = 'world'; session_write_close(); // session is now closed, it's safe to redirect // if not closed, $_SESSION['hello'] may not be set properly // when loading the page again // (in this very case, calls to this script could loop for a while) header('Location: ' . $_SERVER['PHP_SELF']); } ?>

« previous php.notes (#208265) next »