69

I'm a noob programmer so I apologies in advance for any obvious mistakes. I've spent the past week creating a product database kinda thing. I've got too the point where I can add products using a form, view all products added etc. I've being using sessions which are created via the form input data. I'm struggling to include get a delete product page working, I've tried using unset to clear the variable but can't get it too work.

ADD Product page which sets the session variable:

$_SESSION['Products'][] = $_POST; //is how i set the session on the add products page. 

unset $_SESSION['Products'][]; //is how i have tried to clear the session although it does not work.

Any point in the right direction will be appreciated!

9
  • 15
    unset($_SESSION['Products']); is enough Commented Jun 2, 2016 at 8:34
  • 2
    and to clear all session's use session_unset(); Commented Jun 2, 2016 at 8:37
  • Giving it a bit of thought I think I would need to do more than unset the session variable. I would need to delete the entire array for that product from my product list page. My bad Commented Jun 2, 2016 at 8:41
  • t3kz I think that would delete the entire list of products rather than one, i'll have a go Commented Jun 2, 2016 at 8:42
  • @BradleyBoothman so using unset($_SESSION['Products']); fixes right? Commented Jun 2, 2016 at 8:45

9 Answers 9

146

You can unset session variable using:

  1. session_unset - Frees all session variables (It is equal to using: $_SESSION = array(); for older deprecated code)
  2. unset($_SESSION['Products']); - Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)
  3. session_destroy — Destroys all data registered to a session

To know the difference between using session_unset and session_destroy, read this SO answer. That helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Only use session_unset() for older deprecated code that does not use $_SESSION.
What exactly is deprecated code that does not use $_SESSION ? Are there (deprecated) ways to use sessions in PHP other than $_SESSION ?
unset($_SESSION['Products']); this works for me. thx
18

I am including this answer in case someone comes to this page for the same reason I did. I just wasted an embarrassing amount of time trying to track the problem down. I was calling:

unset($_SESSION['myVar']);

from a logout script. Then navigating to a page that required login, and the server still thought I was logged in. The problem was that the logout script was not calling:

session_start();

Unsetting a session var DOES NOT WORK unless you start the session first.

Comments

14

Unset is a function. Therefore you have to submit which variable has to be destroyed.

unset($var);

In your case

unset ($_SESSION["products"]);

If you need to reset whole session variable just call

session_destroy ();

3 Comments

Nitpicking here: unset is a language construct and not a function.
Yes. It is a language construct. But php manual itself categorize unset as a variable handling function as well. www.php.net/manual/en/ref.var.php . Any way I just needed to highlight the fact that variable name should be passed to it.
Yes but the difference is there. It's not a function.
11

If you completely want to clear the session you can use this:

session_unset();
session_destroy();

Actually both are not neccessary but it does not hurt.

If you want to clear only a specific part I think you need this:

unset($_SESSION['Products']);
//or
$_SESSION['Products'] = "";

depending on what you need.

1 Comment

$_SESSION["XYZ"] = ""; will not clear the memory. The variable will still exist, and isset() will still return true on it
3

unset is a function, not an operator. Use it like unset($_SESSION['key']); to unset that session key. You can, however, use session_destroy(); as well. (Make sure to start the session with session_start(); as well)

1 Comment

unset is not a function, but a language construct
3

Destroying a PHP Session

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

Here is the example to unset a single variable

<?php    unset($_SESSION['counter']); ?>

Here is the call which will destroy all the session variables

<?php    session_destroy(); ?>

Comments

1
// set
$_SESSION['test'] = 1;

// destroy
unset($_SESSION['test']);

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0
$_SESSION['Poducts'] = 1; // set
unset($_SESSION['Products']); //unset
        

Comments

0

All the answer about unset are correct but one thing is needed to be corrected. If you did not use session_start() the unset() will never work. I recommend doing it this way

session_start();
unset($_SESSION['productID']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.