65

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!

Ali
  • 1,408
  • 10
  • 17
bbowesbo
  • 885
  • 1
  • 7
  • 17

9 Answers9

138

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.

Jsowa
  • 9,104
  • 5
  • 56
  • 60
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
15

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.

Jeff Martin
  • 409
  • 5
  • 4
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 ();
Don D
  • 726
  • 1
  • 9
  • 19
  • 6
    Nitpicking here: `unset` is a language construct and *not* a function. – PeeHaa Jun 02 '16 at 08: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 Jun 02 '16 at 09:04
  • Yes but the difference is there. It's not a function. – PeeHaa Jun 02 '16 at 09: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.

JRsz
  • 2,891
  • 4
  • 28
  • 44
  • $_SESSION["XYZ"] = ""; will not clear the memory. The variable will still exist, and isset() will still return true on it – clockw0rk Oct 07 '20 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)

mehulmpt
  • 15,861
  • 12
  • 48
  • 88
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(); ?>
Amin
  • 342
  • 4
  • 15
1
// set
$_SESSION['test'] = 1;

// destroy
unset($_SESSION['test']);
David Buck
  • 3,752
  • 35
  • 31
  • 35
RahAlain
  • 11
  • 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. – Donald Duck Jun 19 '20 at 17:11
0
$_SESSION['Poducts'] = 1; // set
unset($_SESSION['Products']); //unset
        
Omar Saade
  • 320
  • 2
  • 8
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']);
helvete
  • 2,455
  • 13
  • 33
  • 37
Unyime
  • 3
  • 3