Viral Bhoot
Viral Bhoot

Reputation: 357

Session array not unset

I want to unset session array but It is not happening. Where am I wrong in this?

if(isset($_SESSION['id']) && isset($_POST['processorder']))
{

    $chk = $_SESSION['id'];

    $query="update order_details set process_order='1' where id IN(".implode(',',$chk).")";
    mysql_query($query) or die(mysql_error());

    unset($chk);

}

Upvotes: 1

Views: 119

Answers (5)

Stas Parshin
Stas Parshin

Reputation: 1673

Unsetting $chk doesn't actually affect the original array. You could use references to do that. But keep in mind that unsetting a reference itself doesn't affect the original array either. This should work:

$session = &$_SESSION;
unset($session[id']);

This could be useful if you need to perform a number of actions on the $_SESSION - should make your code a bit more readable

Upvotes: 0

Shahzad Barkati
Shahzad Barkati

Reputation: 2526

Use NULL and unset to remove the saved values in $_SESSION[] variables, like:

$_SESSION['id'] = NULL;
unset($_SESSION['id']);

If you put

$_SESSION['id'] = ''; 

and in case there is space in between '' two quotes, then

if(isset($_SESSION['id'])){} 

returns true instead of false.

NOTE: You can also use session_destroy() function, but it destroy all saved session data.

Upvotes: 0

Akshay
Akshay

Reputation: 2229

$chk is just a reference varialble. You need to unset the original variable.

unset($_SESSION['id']);

Upvotes: 0

Nirnae
Nirnae

Reputation: 1345

$chk = $_SESSION['id'];

What you're doing here is creating a variable $chk with the value $_SESSION['id'] then unsetting this $chk but you've never touched the $_SESSION var.

To do so you need to have the following code :

$_SESSION['id'] = '';
unset($_SESSION['id']);

Upvotes: 3

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

use php unset like that :-

unset($_SESSION['id']);
unset($_SESSION['processorder']);

or you can use session_destroy()

will delete ALL data associated with that user.

Not this unset($chk);

Upvotes: 2

Related Questions