Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all session variables without getting logged out

I am trying to clear all of the session variables but not logout the current user.

user = request.session.get('member_id', None)
request.session.flush()
request.session.modified = True
request.session['member_id'] = user
request.session.modified = True

Will this also affect other users of the site?

like image 923
Siecje Avatar asked Apr 16 '13 14:04

Siecje


People also ask

How can I delete session without logout?

if u use, session_destroy() then most likely ull have to use session_start all over again. Now, if u plan to use another session without having a logout process, then just simply add session_unset(); This will clear ALL $_SESSION variables.

How do I destroy all session variables?

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.

How do I delete all session variables stored in the current session?

Which function is used to erase all session variables stored in the current session? Explanation: The function session_unset() frees all session variables that is currently registered.

Should I destroy session logout?

In fact, it is not necessary, but it's about security, and it's not recommended to leave any data when we don't need it. To destroy a session, you can use session_destroy() , but, as it's said in official docs, it does not unset any of the global variables associated with the session, or unset the session cookie.


1 Answers

As of Django 1.8, any call to flush() will log out the user. From the docs:

Changed in Django 1.8: Deletion of the session cookie is a behavior new in Django 1.8. Previously, the behavior was to regenerate the session key value that was sent back to the user in the cookie.

If you want to be able to delete keys but keep the user logged in, you'll need to handle it manually:

for key in request.session.keys():
    del request.session[key]

Or just delete the specific keys that are of concern:

del request.session['mykey']
like image 54
shacker Avatar answered Oct 12 '22 01:10

shacker