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
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..

Link to comment
Share on other sites

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" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.