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?
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.
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.
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.
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.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With