pyrodude Posted July 14, 2007 Share Posted July 14, 2007 I'm doing some playing around with sessions, and I'm having trouble closing the session. Here's my code: <html> <head> </head> <body> <?php if ($_POST[clear]) { session_destroy(); die; } if ($_SESSION) { session_start(); echo "session started<br>"; $_SESSION[file] = "none"; } else { echo "session already exists<br>"; $_SESSION[file] = $_POST[file]; echo "$_SESSION[file]<br>"; } //Establish which is the current directory (Just the current folder) and set to $value $dir = explode("\\",getcwd()); $value = array_pop($dir); echo $value; //Create a drop-down list with all of the files in the current directory (housing edit.php) echo "<form method=post><select name=\"file\" size=1><option value=\"none\">File to open:</option><option value=\"none\">---------</option>"; $dir2 = scandir("..\\$value"); foreach ($dir2 as $value2) { if (is_file($value2)) { echo "<option>$value2</option>"; } } echo "</select><input type=\"submit\" name=\"open\" value=\"Open\">"; echo "<br><br><input type=\"submit\" name=\"clear\" value=\"Close session\"></form>"; ?> </body> </html> Running as-is, the page works fine until I click the "Close session" button, and get Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in C:\xampp\htdocs\btest\dirlist.php on line 7 I did some searching online and heard mention that it session_start() needs to be called again prior to session_destroy(), in which case I'm getting the error "Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\btest\dirlist.php:5) in C:\xampp\htdocs\btest\dirlist.php on line 7" Any input would be much appreciated! Link to comment https://forums.phpfreaks.com/topic/59981-solved-session_destroy-issue/ Share on other sites More sharing options...
Caesar Posted July 14, 2007 Share Posted July 14, 2007 <?php $_SESSION = ''; unset($_SESSION); ?> Link to comment https://forums.phpfreaks.com/topic/59981-solved-session_destroy-issue/#findComment-298322 Share on other sites More sharing options...
sinisake Posted July 14, 2007 Share Posted July 14, 2007 session_start() must be on the TOP of the code, before ANY other output. Link to comment https://forums.phpfreaks.com/topic/59981-solved-session_destroy-issue/#findComment-298325 Share on other sites More sharing options...
Caesar Posted July 14, 2007 Share Posted July 14, 2007 And yes...before you can do anything with the session variables, you need to initialize the session first.. <?php if(isset($_POST['clear'])) { session_start(); unset($_SESSION); } ?> Link to comment https://forums.phpfreaks.com/topic/59981-solved-session_destroy-issue/#findComment-298326 Share on other sites More sharing options...
pyrodude Posted July 14, 2007 Author Share Posted July 14, 2007 Thanks, guys. My final (working) code is as follows: <?php if (isset($_POST[clear])) { session_start(); unset($_SESSION); session_destroy(); echo "Session destroyed<br>"; select_folder(); } else { if ($_SESSION) { session_start(); echo "<html><head></head><body>"; echo "session started<br>"; $_SESSION[file] = "none"; select_folder(); } else { echo "<html><head></head><body>"; echo "session already exists<br>"; $_SESSION[file] = $_POST[file]; echo "$_SESSION[file]<br>"; select_folder(); } } function select_folder() { //Establish which is the current directory (Just the current folder) and set to $value $dir = explode("\\",getcwd()); $value = array_pop($dir); echo $value; //Create a drop-down list with all of the files in the current directory (housing edit.php) echo "<form method=post><select name=\"file\" size=1><option value=\"none\">File to open:</option><option value=\"none\">---------</option>"; $dir2 = scandir("..\\$value"); foreach ($dir2 as $value2) { if (is_file($value2)) { echo "<option>$value2</option>"; } } echo "</select><input type=\"submit\" name=\"open\" value=\"Open\">"; echo "<br><br><input type=\"submit\" name=\"clear\" value=\"Close session\"></form>"; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/59981-solved-session_destroy-issue/#findComment-298362 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.