Jump to content

session problem!


robcrozier

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.