Re: session_start... twice?

From: Date: Fri, 25 Oct 2002 10:46:06 +0000
Subject: Re: session_start... twice?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-121408@lists.php.net to get a copy of this message
This has to do with how php works with session internaly. $_SESSION is set after the fist call to session_start(), session_destroy() destroys only data that are already stored, the second call to session_start() tries to retrieve these data, but there are none. Use $_SESSION=array() before session_destroy() as the documentation sugests James Taylor wrote:
I have a single page site I'm working on that displays the contents of your current session. Users can clear the contents by clicking a link that triggers a $PHP_SELF?clear=1 - Before any headers are sent out, the script checks for $_GET['clear'], and if it's set, it does a session_destroy();. For some reason though, the contents of the session are STILL displayed until you do a refresh on the page. However, for some bizarre reason, if I call session_start() at the very beginning, call session_destroy(); and then session_start() AGAIN after the destroy, it seems to work like it's supposed to. Is this a bug, or is it working properly? Also - I'm sure there's a better method than this - Any suggestions on what that might be? If there are no better methods well... Are there any downsides to what I'm doing? Below is a "sample" version of the script, to give you an idea of what I mean incase my ramblings above didn't make sense. Thanks!. <?php
session_start();                // Start the session
if (isset($_GET['clear'])) { session_destroy(); // If they chose to clear the session, then we do so } session_start(); // Start it again; it works for whatever reason ?> <html><body> (some other stuff) <? /* Here I just print out whatever's in the session. Unless session_start is called the second time after the destroy, a user will have to click refresh before the data appears to be gone */ if (isset($_SESSION['states'])) { foreach ($_SESSION['states'] as $value) {
     echo "$value<br/>\n";
} } // and lastly, here's our "clear session" link below ?> <a href="index.php?clear=1">Clear the session</a> </body></html>


« previous php.general (#121408) next »