Skip to content
Advertisement

PHP Unset Session Variable

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!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement