Jump to content

Recommended Posts

Does anyone know if it is possible at all to delete all session variables within a session except maybe on or two specified ones.  In other words i want to be able to delete like 30 session variables in the easiest possible way.  i thought maybe you could execute a statement that deletes all session variables within the session except one or two that you could specify in this statement.
Link to comment
https://forums.phpfreaks.com/topic/27773-session-problem/
Share on other sites

[code]<?php

//the function recieves an array of session keys not to delete
function delete_session ($dont_delete)
{
foreach($_SESSION as $key => $val)
{
if(!in_array($key, $dont_delete))
unset($_SESSION[$key]);
}
}

?>[/code]

You need to give the funtion an array with the values you dont want to unset, can be an empty array.

Orio.
Link to comment
https://forums.phpfreaks.com/topic/27773-session-problem/#findComment-127055
Share on other sites

If you know which session variables you want to save, save them in temporary variables, use the [url=http://www.php.net/session_unset]session_unset()[/url] function to unset all the session variables and then recreate the one's you saved.

For example:
[code]<?php
session_start();
$save_these = array('ses_var_1','ses_var_2','last_saved');
foreach ($save_these as $k)
    $save_these[$k] = $_SESSION[$k];
session_unset();
foreach ($save_these as $k => $v)
    $_SESSION[$k] = $v;
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/27773-session-problem/#findComment-127056
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.