68

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
    – Thamilhan
    Commented Jun 2, 2016 at 8:34
  • 2
    and to clear all session's use session_unset();
    – Bram B
    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
    – bbowesbo
    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
    – bbowesbo
    Commented Jun 2, 2016 at 8:42
  • @BradleyBoothman so using unset($_SESSION['Products']); fixes right?
    – Thamilhan
    Commented Jun 2, 2016 at 8:45

9 Answers 9

142

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.

3
  • 2
    Only use session_unset() for older deprecated code that does not use $_SESSION. Commented Mar 28, 2020 at 13:57
  • What exactly is deprecated code that does not use $_SESSION ? Are there (deprecated) ways to use sessions in PHP other than $_SESSION ?
    – Chuck
    Commented Mar 10, 2021 at 11:50
  • unset($_SESSION['Products']); this works for me. thx
    – cerkes
    Commented Oct 1, 2022 at 13:32
17

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.

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
  • 6
    Nitpicking here: unset is a language construct and not a function.
    – PeeHaa
    Commented Jun 2, 2016 at 8:56
  • 1
    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.
    – Don D
    Commented Jun 2, 2016 at 9:04
  • Yes but the difference is there. It's not a function.
    – PeeHaa
    Commented Jun 2, 2016 at 9:06
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
  • $_SESSION["XYZ"] = ""; will not clear the memory. The variable will still exist, and isset() will still return true on it
    – clockw0rk
    Commented Oct 7, 2020 at 15:10
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
  • 2
    unset is not a function, but a language construct
    – PeeHaa
    Commented Jun 2, 2016 at 9:04
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(); ?>
1
// set
$_SESSION['test'] = 1;

// destroy
unset($_SESSION['test']);
1
  • 2
    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. Commented Jun 19, 2020 at 17:11
0
$_SESSION['Poducts'] = 1; // set
unset($_SESSION['Products']); //unset
        
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']);

Not the answer you're looking for? Browse other questions tagged or ask your own question.