note 109981 added to session.examples.basic

From: Date: Fri, 07 Sep 2012 18:06:38 +0000
Subject: note 109981 added to session.examples.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-190574@lists.php.net to get a copy of this message
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']); } ?> ---- Server IP: 72.55.140.6 Probable Submitter: 24.200.48.223 ---- Manual Page -- http://www.php.net/manual/en/session.examples.basic.php Edit -- https://master.php.net/note/edit/109981 Del: integrated -- https://master.php.net/note/delete/109981/integrated Del: useless -- https://master.php.net/note/delete/109981/useless Del: bad code -- https://master.php.net/note/delete/109981/bad+code Del: spam -- https://master.php.net/note/delete/109981/spam Del: non-english -- https://master.php.net/note/delete/109981/non-english Del: in docs -- https://master.php.net/note/delete/109981/in+docs Del: other reasons-- https://master.php.net/note/delete/109981 Reject -- https://master.php.net/note/reject/109981 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190574) next »