user3241754
user3241754

Reputation: 41

PHP: Destroying SESSIONS

I am using php for server side. How do you destroy one session without destroying another session. Let me explain. I created a form where instead of using regular variables I'm using session variables. When the form is submitted I was using a session_destroy() at the end of the post so to clear the page but it also logs me out destroying the log in session. How could I just destroy the forms session variables without destroying the log in session. Sorry for being real noobish.

Upvotes: 0

Views: 93

Answers (6)

Shahbaz
Shahbaz

Reputation: 3463

you can simply use unset to clear a specific session

unset($_SESSION['session name here']);

Upvotes: 0

Vignesh
Vignesh

Reputation: 1063

to avoid many session unset() you may use like this.

<?php
$_session["form_values"]["data1"]=form data1;
$_session["form_values"]["data2"]=form data2;
$_session["form_values"]["data2"]=form data3;
?>

after saved the value, just unset like this.

<?php
unset($_session["form_values"]);
?>

Hope this saves you.

Upvotes: 2

Roland Spoutil
Roland Spoutil

Reputation: 1

you can use unset($_SESSION['var']);

Upvotes: 0

Liam Sorsby
Liam Sorsby

Reputation: 2982

The function session_destroy() will remove the session completely.

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

use unset($_SESSION['session_var']);

unset() destroys the specified variables.

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

Upvotes: 0

Rikesh
Rikesh

Reputation: 26421

session_destroy() destroys all of the data associated with the current session.

What you need is unset to clear any specific session with specifying it's key like:

unset($_SESSION['your_vars']);

Reference.

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146410

You can remove session variables like any other PHP variable:

unset($_SESSION['whatever']);

Upvotes: 0

Related Questions