For a web application, I decided to store the sessions variables into a database. Everything is working like I want except for the logout, in which I thought that with a simple session_destroy() will do the job but is not.
As I mention all the session information is stored in the database, but I notice that when calling session_destroy(); it does delete the session from the database but is not deleted from the server. I notice that if I do not close the browser, every time I loggin I get the same session id, therefore if I loggin with different username, I still get the same session id. How can I delete server session?
This is a function within a class that I use to destroy the session:
function destroy( $id ) {
// Build query
$newid = mysql_real_escape_string($id);
$sql = "DELETE FROM sessions_table WHERE session_id = '{$newid}'";
mysql_query($sql);
return TRUE;
}
Any advice please?
php-manual:
"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."
Related
session_destroy() destroys session data but does not unset any of the global variables associated with session or unset the session cookie.
So why should we destroy session?
Can we destroy a session at the end of page each time the session starts in the beginning of that page giving the same functionality without destroying as well?
session_destroy() will delete the session file (if file storage is used). Otherwise the session file will reside on the server until the garbage collection deletes it. So, if you want to make sure that the stored session data is removed from the server you have to call session_destroy().
Do not call this on every page! Only after the user logs out and you do not need the stored information anymore.
Your correct approach should be to run session_destroy, and then reload the page to force the session changing actions (such as cookie deletion) to work and then the session data in PHP reloads and renews upon page reload.
Before running session destroy you should also "manually" clean the session as well so:
<?php
session_start();
if(count)$_SESSION > 0) {
// Or some other more specific cursory check if the session is populated
$_SESSION = array("","","","");
session_destroy();
header("Location: thispage.php");
exit;
}
...
Page continues....
Also please reference this answer as to how to remove session cookies on the client browser.
hey guys gt into a small problem...I am developing a quiz application ,I need to have a different session for every quiz that is being played...
session_name("random name")
session_start();
this helps me to do the work,but I have got another session being started at login page
I need to destroy the random name session once the quiz is complete
P S:both are two different session
Check the manual on that one: http://php.net/manual/en/function.session-destroy.php
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.
I believe, that you cannot have two sessions active at the same time. If you need to store your quiz values in the session, then you'll need to prefix their indexes so they are handy and easily destroyed. You could have something like:
$_SESSION['Quiz']['Question1'] = "Yes";
$_SESSION['Quiz']['Question2'] = "No";
then when your finished with the Quiz with
unset($_SESSION['Quiz']);
In PHP manual, the description for session_destroy() function is :
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.
I am confused about this description. If this function destroys all session data, then why the global variables associated with the session are not unset? Why can we use the session variables again?
I am confused about this description. If this [session_destroy()] function destroys all session data, then why the global variables associated with the session are not unset? Why can we use the session variables again?
Session data is the data associated with the session. The session is defined by its name (the session name) and its id (the session id).
By using that function, all this sessions (name + id) data is destroyed.
The variable container which allowed you to read / set that data is still there, so you can operate on that data (e.g. there might be information in like last activity and this is a logout and you want to store the last activity know at logout or so into some logs or database, so why delete it? that would be counter productive because you want to destroy (or commit) sessions fast, e.g. when you know read-only access is needed only, keep the session data in memory, but commit the session already because there is no need to keep it open).
Keep in mind that even these variables are access via $_SESSION they are not part of the session any longer. Perhaps that is the confusing part?
BTW my description is not totally correct. PHP internally identifies the session data by the id only so you could change the session name and session_destroy() would still remove the session data because the session id has not changed.
session_destroy() deletes the session file where session data are stored. Look here:
<?php
session_save_path('./session/');
session_start();
$_SESSION['v'] = array( 'foo' => 123, 'bar' => 'spam' );
$_SESSION['m'] = "rocky";
if( isset($_GET['delete']) == 'true' )
session_destroy();
?>
I have a script whitch creates a session and set the value of v to 10, and it saves the session data in the same script path in a folder named ./session.
Now open the page and then browse the ./session directory, you should see a file with name similar to sess_4r7ldo7s5hsctu3fgtvfmf4sd0. This is where session data is being stored and it will contains:
v|a:2:{s:3:"foo";i:123;s:3:"bar";s:4:"spam";}m|s:5:"rocky";
Activate session_destroy() by passing ?delete=true to the page, the session file will be simply deleted.
I have two users in mysql database, when one user is logged in, it gets a session id. But when 1st user logs out & 2nd user logs in it gets the same session id as of the 1st user. I want that even if the browser is not closed, but there are multiple login & logouts from the same browser, the session id should change for every user who logs in.
i use the following code :
session_unset();
session_destroy();
Then you need to explicitly destroy the session or regenerate the id.
I'm guessing you're currently just leaving it hanging there.
use session_destroy() on your log-out button.
Call session_destroy() in your logout script.
You can also call it in the login script, if a new user logs in without the old user logging out.
From the documentation:
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.
When I try to log out the session is destroyed but I still can go inside that page and view details without logging in first by using the Mozilla browser back button or history cache.
code for logout is
<php
session_start();
session_unset($_SESSION['user']);
//redirect to login page
header('location:login.php');
session_write_close();
?>
members page.
<php
if(!isset($_SESSION['user'])||(trim($_SESSION['user']==''))){
require('error.php');
}
else{
require('view.php');
//the function queries the db.
member_detail($user,$password);
}
In this code if I use the link to the page, it goes to the error page but if I log in, member details is displayed since the session is active so problem is after logout.
first make sure your session is destroyed using session_destroy function or unset the whole session array.
and in print the session array in test page after logout. this will give you which session variables are there. use isset method to check whether or not session variables exist.
Sometimes session_unset and session_destroy does not clear the session data.
Reference: http://www.dmxzone.com/forum/topic/14240/
I have similar experience. Perhaps it is because of not using the methods properly.
Quickfix:
if you want to unset a particular session variable:
$_SESSION["variable"]="";
That will 'unset it'
To unset the whole SESSION
$_SESSION=array();
I seriously do NOT know how valid these are as recommended programming practices, however, they work for me.
FROM the manuals
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.
and
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.
Perhaps other users can add more to this answer. Plus the manuals at php.net have very informative comments with sample code.