Jump to content

session_destroy() not working


fishbaitfood

Recommended Posts

Hey phpfreaks,

 

I have a session, to add words to an array. But my session_destroy() won't work at the end!

 

This is my code:

 

//at the very top of my index.php
<?php
session_start();
header('Content-type: text/html; charset=UTF-8');
?>

//then some html and javascript code (not affecting my array)
//........
//........


//then my actual form and php code, for adding a word to an array

<form id="arrayForm" method="post" action="index.php">
<input type="text" name="word" maxlength="12" />
<input class="btn" type="submit" name="submit" value="submit" />
<input class="btn" type="reset" name="reset" value="clear array" />
</form>

<p>
<?php
  function sanitize($input) {
   if (!empty($input)) {
    $input = strip_tags($input);
    $input = htmlentities($input);

    return trim($input);
   }
   else {
    return FALSE;
   }
  }

  if (isset($_POST['submit'])) {
   $word = $_POST['word'];
					  
   if (sanitize($word)) {
    $_SESSION['words'][] = $word;
    //display words in array - this works
    for($i=0; $i < count($_SESSION['words']); $i++){
     echo $_SESSION['words'][$i] . " ";
    }
   }
   else {
    echo 'Please enter a word';
   }
  }
  //the below won't work
  elseif (isset($_POST['reset'])) {
   $_SESSION['words'] = '';
   unset($_SESSION['words']);
   session_destroy();
  }
?>
</p>

Link to comment
https://forums.phpfreaks.com/topic/209119-session_destroy-not-working/
Share on other sites

How do you know it "doesn't work" and what does that mean?

 

because if I add another word, after I pressed the reset button, the previous words still appear.. It should be ONE new word..  :shrug:

 

It just seems that my 'reset' button doesn't work, but I used it just like my 'submit' button, only difference is that it "clears" the array.. which it doesn't..

An input type="reset" doesn't actually send anything to the server. It simply resets the form dynamically without any http requests. You need to send information to the server telling it to reset.

 

Yes, so something like this with your existing code:

 

<input class="btn" type="submit" name="reset" value="clear array" />

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.