Jump to content

[SOLVED] Session_destroy issue


pyrodude

Recommended Posts

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.